Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
repeating a user_input
#1
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....
Reply
#2
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
Reply
#3
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 ?
Reply
#4
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!")
Reply
#5
okay it seems pretty similiar, where is my mistake ?

ah okay, i think i got it...
Reply
#6
(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
Reply
#7
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...
Reply
#8
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".
ibreeden 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
Reply
#9
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 ?)
ibreeden likes this post
Reply
#10
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is 2/3 not just .666 repeating? DocFro 4 712 Dec-12-2023, 09:09 AM
Last Post: buran
  if else repeating Frankduc 12 2,521 Jul-14-2022, 12:40 PM
Last Post: Frankduc
  matching a repeating string Skaperen 2 1,257 Jun-23-2022, 10:34 PM
Last Post: Skaperen
  Random Number Repeating Tzenesh 5 4,057 Jan-13-2021, 10:00 PM
Last Post: deanhystad
  factorial, repeating Aldiyar 4 2,815 Sep-01-2020, 05:22 PM
Last Post: DPaul
  number repeating twice in loop JonnyEnglish 3 3,322 Nov-24-2019, 09:23 AM
Last Post: ThomasL
  Repeating equations Tbot100 2 3,284 May-29-2019, 02:38 AM
Last Post: heiner55
  First non-repeating char and some errors. blackknite 1 2,284 Jan-06-2019, 02:19 PM
Last Post: stullis
  repeating for loop Kaldesyvon 5 3,867 Dec-06-2018, 08:00 PM
Last Post: ichabod801
  Repeating same number in sequence Rudinirudini 6 4,252 Oct-28-2018, 07:44 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020