Python Forum

Full Version: repeating a user_input
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
okay, so i have this code:

import random

RNumber = random.randint(0,10)

user_input = int(input("please guess a number between 0 and 10: \n"))

if user_input > RNumber:
        print("the number you chose is greater than the random number, pleasr try again\n")
elif user_input < RNumber:
        print("the number you chose is smaller than the random number, pleasr try again\n")
and so far it works...
but what i wanna do is call a user_input all over again when the number that was chosen is not equal the random generated number...kind of a loop probably - but i'm not certain of how to do it....
Use a while: loop.

while user_input != RNumber:
okay, i did the following:

import random

RNumber = random.randint(0,10)

user_input = int(input("please guess a number between 0 and 10: \n"))

while user_input != RNumber:
    user_input = int(input("please guess a number between 0 and 10: \n"))
if user_input > RNumber:
        print("the number you chose is greater than the random number, pleasr try again\n")
elif user_input < RNumber:
        print("the number you chose is smaller than the random number, pleasr try again\n")
elif user_input == RNumber:
    print("the number you chose is correct (", RNumber, ")")
which works in one sense, but it does not give the feedback of printing the greater than or smaller than part of the if and elif,
what is the cause ?
About the only while I use in Python is "while True".
import random

rnumber = random.randint(0, 10)
prompt = "Enter a number from 0 to 10: "
while True:
    unumber = int(input(prompt))
    if unumber > rnumber:
        prompt = "Guess a lower number: "
    elif unumber < rnumber:
        prompt = "Guess a higher number: "
    else:
        break
print("That's it!")
This does the same thing
import random

rnumber = random.randint(0, 10)
unumber = int(input("Enter a number from 0 to 10: ")
while unumber != rnumber:
    if unumber > rnumber:
        unumber = int(input("Guess a lower number: "))
    elif unumber < rnumber:
        unumber = int(input("Guess a higher number: "))
print("That's it!")
okay it seems pretty similiar, where is my mistake ?

ah okay, i think i got it...
(Oct-25-2022, 04:30 PM)astral_travel Wrote: [ -> ]okay it seems pretty similiar, where is my mistake ?

You've maybe figured: all the branches need to be inside the while loop.

I didn't post a full solution, as I thought you'd maybe want to figure it out for yourself.
okay i tried the following:

import random

RNumber = random.randint(1,10)

user_input = int(input("please guess a number between 0 and 10: \n"))

while user_input != RNumber:
    if user_input > RNumber:
        user_input = int(input("choose a lower number: \n"))
    elif user_input < RNumber:
        user_input = int(input("choose a higher number: \n"))
    elif user_input == RNumber:
        print("the number you chose is correct (", RNumber, ")")
but for some reason - when the user_input and RNumber are equal - it does not print the "the number you chose is correct" line...
To understand this, you need to understand that the while loop will exit if the user inputs the correct number.

It's like: "do the following while the input is not equal to the random number".
okay, you're right, i did the following and it worked:

import random

RNumber = random.randint(1,10)

user_input = int(input("please guess a number between 0 and 10: \n"))

while user_input != RNumber:
    if user_input > RNumber:
        user_input = int(input("choose a lower number: \n"))
    elif user_input < RNumber:
        user_input = int(input("choose a higher number: \n"))

print("the number you chose is correct (", RNumber, ")")
is this correct to do ? (i know it gets the job done, but is it correct to implement it like this ?)
It's not how I would do it; you don't need more than one input() function, as deanhystad demonstrated.
Pages: 1 2