Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
while sentence
#1
hi! I'm a beginner and I found here to help or solving my problems. could you please tell me what's wrong with WHILE code?


import random hads= random.randint(1,99) print (hads) ans=str(input('if Javab is higher please enter k and it is lower please enter b and if it's equal enter d')) print (ans) while ans != 'd': if ans==k: hads=random.randint(1,hads) else: hads=random.randint(hads,99) print (hads) print= ("yes it's true")
nilamo write Mar-19-2021, 09:53 PM:
Python tags are important to preserve indentation. I've added them for you, but please include them going forward.
Reply
#2
Can't say for sure. You need to wrap code in Python tags or the indentation is lost. You should also describe what the code is supposed to do. This can be difficult for others to infer if all they have is the code which is not doing what it is supposed to do.

The way your code is written the loop is going to not execute at all or execute forever. Lets say the user types 'd'.
while 'd' != 'd': if ans==k: hads=random.randint(1,hads) else: hads=random.randint(hads,99) print (hads)
'd' != 'd' is False, so the loop does not run at all and the print(hads) statement executes immediately.

If the user enters 'k', k != 'd' is True, so the loop continues to execute forever. The print statement is never reached.

What is this code supposed to do?
supuflounder likes this post
Reply
#3
Welcome to the forums :)

The syntax highlighting should make it clear, but line 5 is a syntax error. There's a single quote in the middle of the string that ends the string prematurely. Either wrap the string in double quotes, or in triple quotes:
#currently ans=str(input('if Javab is higher please enter k and it is lower please enter b and if it's equal enter d')) # double quotes ans=str(input("if Javab is higher please enter k and it is lower please enter b and if it's equal enter d")) # triple quotes ans=str(input('''if Javab is higher please enter k and it is lower please enter b and if it's equal enter d'''))
Also, the last line will overwrite the print() function with "yes it's true" instead of printing anything. The assignment is almost definitely a typo.

If those don't fix your issue, please share the traceback Python gives, or explain what the issue is if it's logical instead of syntactical.
Reply
#4
You want to play "guess the number"?

Copy and paste this into your Python shell, then write: myApp() in the shell

def myApp(): import random print('What shall the maximum number be?') maxNum = int(input('Enter the maximum number ... ')) hads= random.randint(1,maxNum) my_guess = int(input('Enter a number to guess the number "hads" ... ')) if my_guess == hads: print("You guessed the number first time!! You are a psychic!!") print('Your guess was:', my_guess) exit elif my_guess != hads: print('Wrong! Try again!') while my_guess != hads: my_guess = int(input('Enter a number ... ')) if my_guess == hads: print("You guessed the number!! Byebye!") print(my_guess) elif my_guess > hads: print('You guessed too high!') elif my_guess < hads: print('You guessed too low!')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List / arrays putting in sentence Kurta 3 4,042 Dec-25-2020, 11:29 AM
Last Post: Larz60+
  How to make a telegram bot respond to the specific word in a sentence? Metodolog 2 9,098 Dec-22-2020, 07:30 AM
Last Post: martabassof
  How to match partial sentence in long sentence Mekala 1 2,358 Jul-22-2020, 02:21 PM
Last Post: perfringo
  Remove a sentence if it contains a word. lokhtar 6 9,626 Feb-11-2020, 04:43 PM
Last Post: stullis
  Regex Help for clubbing similar sentence segments regstuff 3 3,451 Nov-20-2019, 06:46 AM
Last Post: perfringo
  how to get all the possible permutation and combination of a sentence in python sodmzs 1 5,537 Jun-13-2019, 07:02 AM
Last Post: perfringo
  Sentence maker help bidoofis 2 3,756 Feb-08-2019, 03:59 AM
Last Post: bidoofis
  wont print last sentence.. mitmit293 2 3,518 Jan-27-2019, 05:38 PM
Last Post: aakashjha001
  Generating all simple sentence possibilities Max_77 3 4,591 Oct-10-2018, 11:35 AM
Last Post: Max_77
  PigLatin Sentence Translator Help 2skywalkers 7 8,000 Jun-23-2018, 12:46 AM
Last Post: 2skywalkers

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.