Guessing game not working coded wrong? [duplicate] - python

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.

Related

I am extremely new to python and I am making a random number guessing game [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed last month.
For right now, I have it so it will print the random number it chose. However, whether I get it wrong or right it always says I am wrong.
here is my code:
import random
amount_right = 0
number = random.randint(1, 2)
guess = input
print(number)
print(
"welcome to this number guessing game!! I am going to think of a number from 1-10 and you have to guess it! Good luck!")
input("enter your guess here! ")
if guess != number:
print("Not quite!")
amount_right -= 1
print("you have a score of ", amount_right)
else:
print("good Job!!")
amount_right += 1
print("you have a score of ",amount_right,"!")
what did I do wrong? I am using Pycharm if that helps with anything.
I tried checking my indentation, I tried switching which lines were the if and else statements (lines 13 - 21) and, I tried changing lines 18 - 21 to elif: statements
guess = int(input())
You need to convert your guess to int and there should be () in input
Also there are 2 input() in your code. One is unnecessary. This can be the code.
import random
amount_right = 0
number = random.randint(1, 2)
print(number)
print(
"welcome to this number guessing game!! I am going to think of a number from 1-10 and you have to guess it! Good luck!")
guess = int(input("enter your guess here! "))
if guess != number:
print("Not quite!")
amount_right -= 1
print("you have a score of ", amount_right)
else:
print("good Job!!")
amount_right += 1
print("you have a score of ",amount_right,"!")

Random number guessing game -- cant figure it out, even when i set the number as 7 and guess 7 it says its wrong. Also problem with function calling [duplicate]

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.

"if" statement not working on input variable [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 4 years ago.
Well, I am a beginner and my variable (guess) input doesn't work with the if statement:
When I put on the input numbers from 0 to 9, I want it prints out Right number!, else it print out the other message:
guess = input("Choose a number between 0-9: ")
if guess <=9 and guess >=0:
print("Right number!")
else:
print("The number you entered is out of range try again!")
The input() function returns a string, not a number (e. g. "127", not 127). You have to convert it to a number, e. g. with the help of int() function.
So instead of
guess = input("Choose a number between 0-9: ")
use
guess = int(input("Choose a number between 0-9: "))
to obtain an integer in guest variable, not a string.
Alternatively you may reach it in 2 statements - the first may be your original one, and the second will be a converting one:
guess = input("Choose a number between 0-9: ")
guess = int(guess)
Note:
Instead of
if guess <=9 and guess >=0:
you may write
if 0 <= guess <= 9: # don't try it in other programming languages
or
if guess in range(10): # 0 inclusive to 10 exclusive

A beginner-made python number guessing game. Please help optimize and resolve issues [duplicate]

This question already has answers here:
How can I check if string input is a number?
(30 answers)
Closed 4 years ago.
I have just started to learn coding. This is a simple number guessing game that I created with Python. It works fine but I feel like the code can be simplified/optimized so it looks more neat. Another thing is that every time I try to input a non-integer, an error occurred. Is there a way to check if the user input is an integer or not and then proceed to the next step only if it is an integer. Thank you in advance!
from random import randint
random_number = randint(1, 10)
guesses_taken = 0
print ("Hello! What is your name?")
guess_name = input()
print ("Well, " + guess_name + ", I am thinking of a number between 1-10.")
while guesses_taken < 4:
print ("Take a guess!")
guess = int(input())
if guess not in range(1, 11):
print ("Oops! That's not an option!")
elif guess == random_number:
print ("You're right! Congratulations!")
break
else:
guesses_taken += 1
if guesses_taken < 4:
print ("Nope! Try again,")
else:
print ("Out of chances. You lose.")
You could use a try except block, to check if the value is an integer and throw an error/user message if not, (this would go where you have guess = int (input())
something like:
try:
val = int(guess)
except ValueError:
print("Please enter an Integer")

Python 3.3.1 While Loops [duplicate]

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.

Categories