Here's the code
# Number guessing game
# Import any libraries here
from random import randrange
#def user_integer():
# ''' get what user's integer here'''
# x = int(input("Enter integer to play"))
def random_int(size):
""" This function takes in an integer size that indicates the
length of the list of integers beginning at 1 and ending
at size, including size. It returns a random integer between 1 and size.
"""
return randrange(1, size 1)
# Test out the function
#random_int(10)
def new_guess():
'''user input for a new number'''
x = input("what's your guess?")
x = int(x)
#while x!= -4:
# compare(user_num, computer_num)
return x
def compare(user_num, computer_num):
''' compares size of numbers and prints winner'''
user_num = new_guess()
computer_num = int(random_int(1000))
if user_num > computer_num:
print ("user wins")
elif user_num == computer_num:
print ("it's a tie")
elif user_num < computer_num:
print ("robots win")
else:
print("wrong input")
def main():
''' control center, allowing me to use multiple functions to pass information '''
# Write your algorithm here
computer_num = random_int(1000)
user_num = new_guess()
print("You guessed", user_num)
print("The robot's number is",computer_num)
compare(user_num, computer_num)
main()
This gives me:
what's your guess? 85 #input
You guessed 85
The robot's number is 540 #random number
what's your guess? 85 #it makes user input again
robots win
Everything prints that is supposed to print. However, since I called user_num = new_guess()
, it makes the user input a number once there and then again for the function compare(user_num, computer_num)
How do I make it so that a user only inputs a number once to get a response from compare(user_num, computer_num)
?
Should I call user_num a new name once I get my guess in def main()?
CodePudding user response:
The problem is you're getting input for twice. In main function, you get computer_num
random number and user_num
input. And then you call compare()
function, which have these both parameters.
You can delete 2 lines of code from compare()
function. It should look like this:
def compare(user_num, computer_num):
''' compares size of numbers and prints winner'''
if user_num > computer_num:
print ("user wins")
elif user_num == computer_num:
print ("it's a tie")
elif user_num < computer_num:
print ("robots win")
else:
print("wrong input")
CodePudding user response:
It's because your calling your new_guess() function two times. Once in main() and another in compare