This question already has answers here:
How to create a loop in Python [duplicate]
(2 answers)
Closed 8 years ago.
I'm trying to write a while loop here is my code (homework so its basic):
import random
RandomNumber=(random.randint(0,100))
GuessedNumber=int(input("Guess any whole number between 0 and 100! "))
while RandomNumber != GuessedNumber:
if GuessedNumber==RandomNumber:
print("Well done you gessed correctly!")
else:
print("Unlucky guess again!")
If anyone knows what I'm doing wrong with my while loop, help would be appreciated; thanks.
You never update the value of GuessedNumber inside the loop. Therefore, if the code enters the loop, it will never leave it because RandomNumber != GuessedNumber will always be true.
You need to do something like this:
import random
RandomNumber=(random.randint(0,100))
GuessedNumber=int(input("Guess any whole number between 0 and 100! "))
while RandomNumber != GuessedNumber:
print("Unlucky guess again!")
GuessedNumber=int(input("Guess any whole number between 0 and 100! "))
print("Well done you gessed correctly!")
Notice how the value of GuessedNumber is now updated with each iteration of the loop.
Related
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 1 year ago.
def guessNum():
guess = input("guess a number between 1 and 10: ")
guesses = 3
randomNum = 7
while guess != randomNum:
guesses -=1
print("wrong," + str(guesses) + "guesses left")
guess = input("guess again ")
if guesses <= 1:
print("You lose")
break
if guess == randomNum:
print("You win")
break
print guessNum()
so im having issues with it saying false when its correct. Also when I create a function, it only executes if I give it an input. why does it require an input? cant it have 0 or guessnum(1) and put a random variable when defining it, ie def guessnum(num):
The problem is that when you input something, it isn't normally an integer. Instead, it's a string. Hence, all you have to do is add the line guess = int(guess) right after the user guesses again.
This question already has answers here:
Limiting user input to a range in Python
(5 answers)
Closed 3 years ago.
I am trying to create a loop where the user is given choices 1-8 and if they do not choose 1-8, it loops them back around re-enter number 1-8. I am attempting to use a while loop with two conditions. What am I missing?
fm_select = int(input("Enter a number 1-8"))
while fm_select <= 8 and fm_select >= 1:
Your ranges are wrong. You want the while loop to fail when they are correct, since you're trying to break out of the loop. So, you want your loop to check against every number that isn't between one and eight. Instead, do
fm_select = 0
while (fm_select < 1 or fm_select > 8):
fm_select = int(input("Enter a number between one and eight: "))
"As long as their input is less than one or higher than eight, keep asking"
something like this should work
while(True):
fm_select = int(input("Enter a number 1-8"))
if 0 < fm_select < 8:
break
print("try again")
print("you have entered %d" %(fm_select) )
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 5 years ago.
Helloo
I'm trying to make a simple game where you have to choose either 1 or 2 and one of them is correct. I've used a simple random generator to choose either 1 or 2 as the correct answer.
def guess():
print("")
print("Select a number, 1 or 2")
print("")
from random import randint
ran = randint(1, 2)
nmr = input("")
if nmr == ran:
print("That's correct!")
else:
print("Wrong number")
Every time I answer it prints "Wrong number".
I've also tried printing the random number before answering but it still takes it as incorrect. Any idea what is wrong there?
The problem is that you are comparing a string with an int. That always gives False.
ran = randint(1, 2) #int
nmr = input("") #str
So for it to work, either convert ran to str or nmr to int
This question already has answers here:
IndentationError: unindent does not match any outer indentation level
(32 answers)
Closed 5 years ago.
I have my line of code for a guessing game. But it tells me that: unindent does not match any outer indentation level, could you help me find other mistakes? Thanks!
number = random.randint(1, 99)
guess = int(raw_input("Enter an integer from 1 to 99: "))
guesses = 0
print ('this is your guess', guess)
if guess < number:
print ('guess is low')
elif guess > number:
print ('guess is high')
elif guess == number:
break
Unless this code chunk is inside some type of loop then the error is the break on the last line. break can only be used on loops and not of if/else.
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 5 years ago.
I am making a small quiz that has different difficulties, when doing my easy section my if statement always prints incorrect even with the correct answer - i am still new to python it might be a very easy thing to solve
How can I know if this is a duplicate if i don't know what is wrong
here is my code
def easy():
score = 0
print("Welcome to the easy section!")
print("****************************")
print("Random Access Memory(1)")
print("Radical Amazonian Mandem(2)")
qe1 = input("What does RAM stand for?: ")
if qe1 == 1:
score = score + 1
print("Well done, next question:")
if qe1 != 1:
print("Incorrect, next question:")
else:
easy()
input function return a string, you should cast it to int:
qe1 = int(input("What does RAM stand for?: "))