This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Closed 5 months ago.
from fileinput import close
from random import Random, random
print('Do you want to play a game?')
input1 = input("yes or no? ")
if input1 == "yes" or input1 == "Yes":
print("You have 3 tries to guess a random integer between 0-10. If you guess right
you win. If not I do. Ready?")
import random
def repeat():
number = random.randint(1,10)
print(number)
guess1 = input('Your first guess: ')
if guess1 == number:
print('You are correct! You only needed one try!')
else:
print('Wrong two tries left!')
guess2 = input('Your second guess: ')
if guess2 == number:
print('You are correct! You needed two tries!')
else:
print('Wrong one try left!')
guess3 = input('Your third and last guess: ')
if guess3 == number:
print('You are correct! It took you all three tries!')
else:
print('You are wrong! You lost! The number was:')
print(number)
input2 = input('Do you want to play again? Yes or No? ')
if input2 == 'Yes' or 'yes':
repeat()
else:
close()
else:
close()
repeat()
I have no clue where the mistake is, but when I guess number correctly it still says its wrong.
You need to convert all guesses into integers to compare since the default input type is string.
from fileinput import close
from random import Random, random
print('Do you want to play a game?')
input1 = input("yes or no? ")
if input1 == "yes" or input1 == "Yes":
print("You have 3 tries to guess a random integer between 0-10. If you guess right you win. If not I do. Ready?")
import random
def repeat():
number = random.randint(1,10)
print(number)
guess1 = int(input('Your first guess: '))
if guess1 == number:
print('You are correct! You only needed one try!')
else:
print('Wrong two tries left!')
guess2 = int(input('Your second guess: '))
if guess2 == number:
print('You are correct! You needed two tries!')
else:
print('Wrong one try left!')
guess3 = int(input('Your third and last guess: '))
if guess3 == number:
print('You are correct! It took you all three tries!')
else:
print('You are wrong! You lost! The number was:')
print(number)
input2 = input('Do you want to play again? Yes or No? ')
if input2 == 'Yes' or 'yes':
repeat()
else:
close()
else: close() repeat()
Related
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 7 months ago.
I'm trying to make a simple number guessing game. I wanted to try to see if I could do this myself without looking up any answers but I'm very confused on how to keep the game going if the guess is not correct. Here is what I have so far:
import random
#ask user to guess a number:
guess = int(input("Guess a number from 0 to 100: \n"))
#create random number:
computer_number = random.randint(0, 100)
#How can i make this block of code loop to keep on giving the user tries??
if guess == computer_number:
print("You won")
elif guess > computer_number:
print("Try a lower number!")
else:
print("Try a higher number!")
import random
#ask user to guess a number:
#create random number:
computer_number = random.randint(0, 100)
#How can i make this block of code loop to keep on giving the user tries??
while True:
guess = int(input("Guess a number from 0 to 100: \n"))
if guess == computer_number:
print("You won")
break
elif guess > computer_number:
print("Try a lower number!")
else:
print("Try a higher number!")
You could use a while loop to loop the game until a condition is met.
For example:
#create random number:
computer_number = random.randint(0, 100)
while True:
#ask user to guess a number:
guess = int(input("Guess a number from 0 to 100: \n"))
#How can i make this block of code loop to keep on giving the user tries??
if guess == computer_number:
print("You won")
break
elif guess > computer_number:
print("Try a lower number!")
else:
print("Try a higher number!")
it's very easy you just need an while loop:
while condition:
#while condition True run what stands here
So the answer to your question is:
import random
#create random number:
computer_number = random.randint(0, 100)
guess = -1
#we need to firs declare the variables so we can use them in the condition
while guess != computer_number:
#ask user to guess a number:
guess = int(input("Guess a number from 0 to 100: \n"))
if guess == computer_number:
print("You won!")
elif guess > computer_number:
print("Try a lower number!")
else:
print("Try a higher number!")
Tried to make a guessing game that counts the number of attempts but I don't know how to count only as one attempt if they input the same number multiple times consecutively
import random
repeat = 'y'
i=1
while repeat.lower() == 'y':
n = int(input("Enter value of n(1 - 100) "))
n2 =int(input("Enter value of n2(1 - 100) "))
a= random.randrange(n, n2)
guess = int(input("Guess the hidden number"))
while guess !=a:
print('entry number:',i)
if guess<a:
print("you need to guess higher. Try again")
guess = int(input("Guess the hidden number"))
if guess>a:
print("you need to guess lower. Try again")
guess = int(input("Guess the hidden number"))
if guess == a:
print('it took you:',i,'tries to guess the hidden number')
print('Congratulations, the hidden number is',a)
repeat = input("\nDo you want to try again Y/N? \n>>> ")
while repeat.lower() == 'n':
print('thank you')
break
This should do it.
You may also want to also store the two numbers in a list and sort the list after getting them so the program does not crash if the second number is lower.
You may want to print out the number of guesses it took when a user gets the correct answer.
import random
repeat = 'y'
i=0
while repeat.lower() == 'y':
n = int(input("Enter value of n(1 - 100) "))
n2 =int(input("Enter value of n2(1 - 100) "))
a= random.randrange(n, n2)
guess = int(input("Guess the hidden number"))
guessList = []
while guess != a:
if guess not in guessList:
print('here')
guessList.append(guess)
i += 1
print('entry number:',i)
if guess<a:
print("you need to guess higher. Try again")
guess = int(input("Guess the hidden number"))
if guess>a:
print("you need to guess lower. Try again")
guess = int(input("Guess the hidden number"))
if guess == a:
print('it took you:',i,'tries to guess the hidden number')
print('Congratulations, the hidden number is',a)
else:
print("Duplicate guess try again.")
guess = int(input("Guess the hidden number"))
repeat = input("\nDo you want to try again Y/N? \n>>> ")
while repeat.lower() == 'n':
print('thank you')
break````
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 1 year ago.
import random
from random import randint
number = randint(1, 500)
guess = input("The computer has chosen a random number. Guess the number: ")
guess = int(guess)
while guess == number:
print("Congrats, you have won")
break
if guess > number:
print("Lower")
if guess < number:
print("Higher")
This code only allows the user to input one guess and then the program ends. Can someone help me fix this
You should think about your loop condition.
When do you want to repeat? This is the loop condition
When the guess is not correct. guess != number
What do you want to repeat? Put these inside the loop
Asking for a guess guess = int(input("Your guess: "))
Printing if it's higher or lower if guess > or < number: ...
What don't you want to repeat?
You need this before the loop
Deciding the correct number.
Setting the initial guess so the loop is entered once
You need this after the loop
Printing the "correct!" message, because you only exit the loop once the guess is correct
So we have:
number = random.randint(1, 100)
guess = 0
while guess != number:
guess = int(input("Your guess: "))
if guess > number:
print("Lower")
elif guess < number:
print("Higher")
print("Correct!")
Right now, your while loop is useless as once you are in it you break immediately. The rest of the code is not in a loop.
You should rather have an infinite loop with all your code, and break when there is a match:
from random import randint
number = randint(1, 500)
while True: # infinite loop
guess = input("The computer has chosen a random number. Guess the number: ")
guess = int(guess) # warning, this will raise an error if
# the user inputs something else that digits
if guess == number: # condition is met, we're done
print("Congrats, you have won")
break
elif guess > number: # test if number is lower
print("Lower")
else: # no need to test again, is is necessarily higher
print("Higher")
You must take input in the loop, because the value for each step has to be updated .
from random import randint
number = randint(1, 500)
while True:
guess = input("The computer has chosen a random number. Guess the number: ")
guess = int(guess)
if guess == number:
print("Congrats, you have won")
break
if guess > number:
print("Lower")
if guess < number:
print("Higher")
I'm working on a Python script where a user has to guess a random number, selected by the script. This is my code:
import random
while True:
number = random.randint(1, 3)
print("Can you guess the right number?")
antwoord = input("Enter a number between 1 and 3: ")
if antwoord == number:
print ("Dang, that's the correct number!")
print (" ")
else:
print ("Not the same!")
print ("The correct answer is:")
print (number)
while True:
answer = input('Try again? (y/n): ')
print (" ")
if answer in ('y', 'n'):
break
print("You can only answer with y or n!")
if answer == 'y':
continue
else:
print("Better next time!")
break
It works... Sort of... I was trying it and came across this:
User enters 2, it says it's incorrect, but then displays the same number!
I have the feeling that, every time I call the variable 'number', it changes the random number again. How can I force the script to hold the random number picked at the beginning, and not keep changing it within the script?
As far as I understand it, you want to pick a new random integer in every loop step.
I guess you are using python 3 and so input returns a string. Since you cannot perform comparisson between a string and an int, you need to convert the input string to an int first.
import random
while True:
number = random.randint(1, 3)
print("Can you guess the right number?")
antwoord = input("Enter a number between 1 and 3: ")
try:
antwoord = int(antwoord)
except:
print ("You need to type in a number")
if antwoord == number:
print ("Dang, that's the correct number!")
print (" ")
else:
print ("Not the same!")
print ("The correct answer is:")
print (number)
while True:
answer = input('Try again? (y/n): ')
print (" ")
if answer in ('y', 'n'):
break
print("You can only answer with y or n!")
if answer == 'y':
continue
else:
print("Better next time!")
break
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 6 years ago.
import random
computer=random.randint(1, 100)
guess=int(input("guess the number"))
if guess > 100:
print("your guess is too high")
elif guess < 1:
print("your guess is too low")
elif guess==computer:
print("well done!")
else:
print("you\'re wrong, guess again")
This is my current code. it's game where the computer randomly chooses a number and the player has to guess it. i have tried but i don't know how to ask the player if they want to play again and if they say yes how to restart it.
Wrap the game code to function and use while True to call it again and again
import random
def play():
computer=random.randint(1, 100)
guess=int(input("guess the number"))
if guess > 100:
print("your guess is too high")
elif guess < 1:
print("your guess is too low")
elif guess==computer:
print("well done!")
else:
print("you\'re wrong, guess again")
while True:
answer = input("do you want to play?")
if answer == 'yes':
play()
elif answer == 'no':
break
else:
print("dont understand")
Put all the if-else statements within one big while loop that keeps looping until the user guesses the number correctly. Then, after each losing outcome, give the user another chance to guess the number so that the loop has a chance to reevaluate the guess with the next iteration. In my modification below I decided to leave the last if-else statement outside of the loop because when the user guesses correctly, the code will break out of the loop to check if the guess is correct. Of course in this scenario it has to be correct so the user is told that he or she is right and the program terminates.
import random
computer=random.randint(1, 100)
guess=int(input("guess the number\n"))
while(guess != computer):
if guess > 100:
print("your guess is too high\n")
guess=int(input("guess the number again\n"))
elif guess < 1:
print("your guess is too low\n")
guess=int(input("guess the number again\n"))
if guess==computer:
print("well done!\n")
Just an overview of how you can do it.
initialize a variable play_again = "yes"
Place a while check on play_again:
while play_again == "yes":
Enclose all if statements and guess input in the while loop
Read the user input in a variable within but at the end of the loop:
play_again = raw_input("\n\nWant to play again?: ")
You can use a while loop for multiple iterations
import random
computer=random.randint(1, 100)
guess=int(input("guess the number"))
play_again = 'y'
while play_again == 'y':
if guess > 100:
print("your guess is too high")
elif guess < 1:
print("your guess is too low")
elif guess==computer:
print("well done!")
else:
print("you\'re wrong, guess again")
play_again = input("Want to play again(y/n): ")
You should put your code in while loop. Then after game ask user if they want to continue with input() function. If they say 'no' you can break the loop or set argument of while to False.