I should preface my question by saying I'm very new to Python (3 days), with no prior experience at programming, so if I don't understand the answers immediately, please bear with me. I'm using Python 3.5.3, and trying to do the following exercise found here (https://www.practicepython.org):
Generate a random number between 1 and 9 (including 1 and 9). Ask the user to guess the number, then tell them whether they guessed too low, too high, or exactly right. Keep the game going until the user types “exit”
So far I got this:
import random
import sys
value=random.randint(0, 10)
x=value
y=input("I thought of a number between 0 and 100. Try to guess what it was! ")
z=type(y)
if z==int:
if y<x:
print("too low!")
elif y>x:
print("too high!")
elif y==x:
print("right!")
elif y==z:
print("thank you for playing!")
sys.exit(0)
elif z==str:
if y=="exit":
print("thank you for playing!")
sys.exit(0)
elif y !="exit":
print("Please write only numbers from 1 to 10 or 'exit'. Thank you.")
else:
print("Please write only numbers from 1 to 10 or 'exit'. Thank you.")
I created the "z" variable to store information about the input type, so as to have do it different things if it's an integer or a string. I felt that is was necessary because I was having a lot of error messages regarding mixing str to ints. I was expecting the program to identify if the user wrote a string, which would trigger the following code:
elif z==str:
if y=="exit":
print("thank you for playing!")
sys.exit(0)
elif y !="exit":
print("Please write only numbers from 1 to 10 or 'exit'. Thank you.")
or a integer, where it would do this:
if z==int:
if y<x:
print("too low!")
elif y>x:
print("too high!")
elif y==x:
print("right!")
elif y==z:
print("thank you for playing!")
sys.exit(0)
In reality what is happening is, whenever I write an input, be it a number or a word, I only get this message:"Please write only numbers from 1 to 10 or 'exit'. Thank you". I commented out the code regarding strings and still I got this answer, then I commented out the string code and the "else" code, which made it exit the program after a number is inputed.
My conclusion, at this moment, is that the program is interpreting all inputs as strings. Previously in the "y" variable I had it written like this:
int(input("I thought of a number between 0 and 100. Try to guess what it was! "))
I changed it to the current form of handlig strings and integers. because I was getting this error message when writing "exit":
ValueError: invalid literal for int() with base 10: 'exit'
Any help would be greatly appreciated.