0

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.

Matthew Story
  • 3,573
  • 15
  • 26
user3710626
  • 2,167
  • 2
  • 11
  • 8
  • Possible duplicate of [Determine the type of an object?](https://stackoverflow.com/questions/2225038/determine-the-type-of-an-object) – Pierre C. Jul 28 '18 at 20:00
  • 2
    Possible duplicate of [How to check if string input is a number?](https://stackoverflow.com/questions/5424716/how-to-check-if-string-input-is-a-number) – Taku Jul 28 '18 at 20:02
  • `y` as a result of [`input`](https://docs.python.org/library/functions.html#input) supposed to be `str`, always – Azat Ibrakov Jul 28 '18 at 20:04

2 Answers2

2
int()

takes a float or string value and makes it an integer for example, 1.0 ---> 1 "1"--->1, and so on. When you input "exit" the computer has to take the integer of that, but of course "exit" is not an integer so it returns that error.

To do what you want, is pretty simple.

try:
    a = int(y)
    if a<x:
        print("too low!")
    elif a>x:
        print("too high!")
    elif a==x:
        print("right!")
    elif a==z:# i dont understand what this is doing, but i will leave it
        print("thank you for playing!")
        sys.exit(0)
except ValueError:
   if y=="exit":
       sys.exit(0)
   else:
       print("Please write only numbers from 1 to 10 or 'exit'. Thank you.")

Side note, in the top you say

random.randint(0,10)

but for your input say a number between 1 and a 100

  • Hi and thanks for answering so quickly. You are right, my string was incerrect, it should read "between 0 and 10". – user3710626 Jul 28 '18 at 20:51
2

input() in python always returns str. You cant do this:

int(input("I thought of a number between 0 and 100. Try to guess what it was! "))

Because if the user enters a string i.e. not numeric, the program can not cast it to int, and hence would throw an error.

You can however use value.isdigit() to check if value(in string) is a valid number or not, however you would still need to cast it to int to use it later in your program.

Here is the working program.

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! ")

if y.isdigit():
    y = int(y)
    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)
else:
    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.")
hsnsd
  • 1,728
  • 12
  • 30