Python Forum
while sentence - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: while sentence (/thread-32958.html)



while sentence - kimyyya - Mar-19-2021

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")



RE: while sentence - deanhystad - Mar-19-2021

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?


RE: while sentence - nilamo - Mar-19-2021

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.


RE: while sentence - Pedroski55 - Mar-20-2021

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!')