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
Related
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 2 years ago.
I am trying to make my program print(invalid input, try again) if the user types a string instead of an integer.
num_dice = 0
while True:
if num_dice < 3 or num_dice > 5 or num_dice:
num_dice = int(input("Enter the number of dice you would like to play with 3-5 : "))
print("")
print("Please enter a value between 3 and 5.")
print("")
continue
else:
break
You can simply use the isnumeric keyword to check if it is an absolute number or not.
Example:
string1="123"
string2="hd124*"
string3="helloworld"
if string1.isnumeric() is True:
#Proceed with your case.
inputs=string1
Documentation reference : https://www.w3schools.com/python/ref_string_isnumeric.asp
P.S. this will require you changing your input to string format, as isnumeric validates only string.
This below part I mean.
num_dice = str(input("Enter the number of dice you would like to play with 3-5 : "))
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 4 years ago.
When I run the below code, it always returns Number please no matter what I enter. Why does this happen?
number = input("please type number: ")
if type(number) != int:
print('Number please')
else:
print('number')
Try this, this should work:
string = input()
if string.isalpha():
print("It is a string. You are correct!")
elif string.isdigit():
print("Please enter a string.")
else:
print("Please enter a proper string.")
The .isalpha is used for checking if it's a string. The .isdigit is used for checking if it's a number.
Hope it helps :)
This question already has answers here:
Python input never equals an integer [duplicate]
(5 answers)
Closed 5 years ago.
Ive looked for awnsers but they dont help.
so my code looks like this:
def creditsyay():
print("Hosted by GitHub")
print("Made by Zeplin-Reaper on GitHub. link: https://github.com/Zeplin-Reapers")
guess()
from random import random
import math
number = random() * 100
number = math.ceil(number)
print(number)
def guess():
uNumber = input("Enter a number: ")
if uNumber == number:
print("correct!")
elif uNumber == "credits()":
creditsyay()
else:
print("Guess Again")
guess()
def main():
guess()
main()
Okay, say Number = 56.
user guesses 5, computer says guess again. Okay.
user then guesses 56.
computer thinks uNumber == number = false
Let me clarify:
computer thinks 56 is not equal to 56
A FIVE YEAR OLD COULD FIND THAT OUT
I need help on how to get the computer to want to do math that a FIVE YEAR OLD could do!!
plz help
EDIT: print(number) is for testing
Replace
uNumber = input("Enter a number: ")
with
uNumber = int(input("Enter a number: "))
I also made a similar program here if you are interested.
https://github.com/brendancreek/Python/blob/master/Guess.py
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?: "))
This question already has an answer here:
Simple Python IF statement does not seem to be working
(1 answer)
Closed 6 years ago.
I have my first program I am trying to make using python. The IF ELSE statement is not working. The output remains "Incorrect" even if the correct number is inputted by the user. I'm curious if it's that the random number and the user input are different data types. In saying that I have tried converting both to int with no avail.
Code below:
#START
from random import randrange
#Display Welcome
print("--------------------")
print("Number guessing game")
print("--------------------")
#Initilize variables
randNum = 0
userNum = 0
#Computer select a random number
randNum = randrange(10)
#Ask user to enter a number
print("The computer has chosen a number between 0 and 9, you have to guess the number!")
print("Please type in a number between 0 and 9, then press enter")
userNum = input('Number: ')
#Check if the user entered the correct number
if userNum == randNum:
print("You have selected the correct number")
else:
print("Incorrect")
On Python 3 input returns a string, you have to convert to an int:
userNum = int(input('Number: '))
Note that this will raise a ValueError if the input is not a number.
If you are using Python 3, change the following line:
userNum = input('Number: ')
to
userNum = int(input('Number: '))
For an explanation, refer to PEP 3111 which describes what changed in Python 3 and why.