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
Related
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,"!")
This question already has answers here:
return statement in for loops [duplicate]
(6 answers)
Closed 1 year ago.
The goal is for the computer to guess a number given an upper and lower bound, the computer should take no as an input and run the program again until a yes is given.
This is the code I am using right now, but the loop only ever runs once dues to where the return function is. If I take that out and jump directly to print it runs continuously.
import random
num1 = int(input("enter your minumum value: "))
num2 = int(input("enter your maximum value: "))
def number_choice(num1,num2):
guess = "no"
while guess == "no":
return random.choice(range(num1,num2))
print (number_choice(num1,num2))
guess = input("is that your number, enter yes or no: ")
Try this:
import random
num1 = int(input("enter your minimum value: "))
num2 = int(input("enter your maximum value: "))
def number_choice(num1,num2):
print(random.choice(range(num1,num2)))
guess='no'
while guess.lower()=='no':
number_choice(num1,num2)
guess = input("Is that your number, enter yes or no: ")
print('Cheers!')
or you can use this too:
import random
num1 = int(input("enter your minimum value: "))
num2 = int(input("enter your maximum value: "))
def number_choice(num1,num2):
print(rnd.choice(range(num1,num2)))
guess=input('Is this your number? Type yes or no ')
if guess=='no':
number_choice(num1,num2)
else:
print('cheers!')
number_choice(num1,num2)
Hope it helps!
This question already has answers here:
How do I determine if the user input is odd or even?
(4 answers)
Closed 4 years ago.
I tried to make a odd/even 'calculator' in python and it keeps popping up errors. Here's the code:
def odd_even():
print("Welcome to Odd/Even")
num = input("Pick a number: ")
num2 = num/2
if num2 == int:
print("This number is even")
else:
print("This number is odd")
Id like to know whats causing the errors and solutions to them
There is an error in the line: num = input("Pick a number: ")
Because input method always returns a String,so you should convert it into int to performs the integer operation
The currect code is:
num =int( input("Pick a number: "))
you can't do math with strings convert it to int
try:
num = int(input("Pick a number: "))
except ValueError:
print('This is not a number!')
return
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 4 years ago.
I am trying to get a user to input a number into a calculator program. The goal right now is to keep giving the user an error message and asking to reenter a number until they enter a real number.
here is some code that I've done:
number_1 = input("Enter your first number here! ")
while number_1 == int():
print("Well done for entering a number!")
you need to use isdigit()
number_1 = input("Enter your first number here! ")
if str(number_1).isdigit():
print("Well done for entering a number!")
How about try: except:?
def my_input(number_1):
while 1:
try:
int(number_1)
return number_1
except:
print("It is not an integer.")
number_1 = input("Give an integer: ")
int1 = input("Enter your first number here! ")
my_input(int1)
print("Well done for entering a number!")
You may have to change int(number_1) to float(number_1) (and input text(s)) depending on your implementation.
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.