Posts: 230
Threads: 39
Joined: Mar 2020
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....
Posts: 453
Threads: 16
Joined: Jun 2022
Use a while: loop.
while user_input != RNumber:
Sig:
>>> import this
The UNIX philosophy: "Do one thing, and do it well."
"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse
"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Posts: 230
Threads: 39
Joined: Mar 2020
Oct-25-2022, 04:25 PM
(This post was last modified: Oct-25-2022, 04:26 PM by astral_travel.)
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 ?
Posts: 6,806
Threads: 20
Joined: Feb 2020
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!")
Posts: 230
Threads: 39
Joined: Mar 2020
Oct-25-2022, 04:30 PM
(This post was last modified: Oct-25-2022, 04:30 PM by astral_travel.)
okay it seems pretty similiar, where is my mistake ?
ah okay, i think i got it...
Posts: 453
Threads: 16
Joined: Jun 2022
Oct-25-2022, 04:40 PM
(This post was last modified: Oct-25-2022, 04:42 PM by rob101.)
(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.
Sig:
>>> import this
The UNIX philosophy: "Do one thing, and do it well."
"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse
"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Posts: 230
Threads: 39
Joined: Mar 2020
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...
Posts: 453
Threads: 16
Joined: Jun 2022
Oct-25-2022, 04:54 PM
(This post was last modified: Oct-25-2022, 04:54 PM by rob101.)
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".
Sig:
>>> import this
The UNIX philosophy: "Do one thing, and do it well."
"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse
"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Posts: 230
Threads: 39
Joined: Mar 2020
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 ?)
Posts: 453
Threads: 16
Joined: Jun 2022
It's not how I would do it; you don't need more than one input() function, as deanhystad demonstrated.
perfringo likes this post
Sig:
>>> import this
The UNIX philosophy: "Do one thing, and do it well."
"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse
"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
|