Oct-07-2021, 12:32 PM
int("Guess the number that was generated from 1-10")This will raise a
ValueError
because "Guess the number that was generated from 1-10"
is not a valid int
.First get the user-input, which is a
str
, then convert it to an int
.user_input = input("Your number: ") user_value = int(user_input) # or in one line user_value = int(input("Your number: "))In line 3 you assign the random int to the name
i
.Don't use characters for names. Use names, which are telling the programmer which object this could be.
For example,
random_value
is a good name.In line 14 you use a bare
elif
without a condition. elif
requires a condition. else
does not require a condition. In line 24 you made it vice versa. else
does not accept a condition. You wanted to use a bare else (because there are no more possibilities) or you could replace else with elif condition
.User input is always error-prone. A user could enter an invalid number, which
int()
can't convert to an int
. This will raise a ValueError
, which ends the program because you're not catching the exception.And the last, but most important is the indentation.
Never use an editor, which indents tabulator instead of 4 spaces.
Never use tab stops in Python source code.
Use a better editor. VS Code, Atom, Code with Mu or other...
PyCharm is too heavy for beginners.
Here the code applied with all suggestions:
import random def ask_user_for_number(): """ Asking the user to input a valid integer and return this value as int. Otherwise repeat the question. """ while True: user_input = input("Guess the number that was generated from 1-10: ") # now the conversion to an int can fail # using exception handling to detect this try: user_value = int(user_input) # int("abc") -> ValueError except ValueError: print("Your input is not an integer:", user_input) # give the user a message and print also his wrong input else: # this block is only executed, if no excption happens # return user_value leaves the function context and the caller gets # user_value return user_value count = 0 # use names which humans can understand random_value = random.randint(1, 11) while True: # here is the call guess = ask_user_for_number() if guess == random_value: # format string. Makes it easier to print text with variables. print(f"Success. The number is {random_value}") # but this won't stop the while True loop # just use break to stop the break out of the loop break elif random_value < guess: print("The number is smaller") else: # this is the last possible case print("The number is greater") # always increment # reduces code duplication # instead of count = count + 1 # you can write count += 1 count += 1 if count == 1: print("You got 4 stars") elif count >= 2: print("You got 3 stars") elif count == 0: print("you got 5 stars")Hint: Use Python 3.10.0 because this version does have better description in exceptions, and sometimes you get hints that you've forgotten a colon.
Hint2: You can put the part on module level, where you print how many stars you got, in a function, which take one argument, which is the count of guesses.
Same code, but in a function:
def star_message(count): if count == 1: print("You got 4 stars") elif count >= 2: print("You got 3 stars") elif count == 0: print("you got 5 stars")Then you can call this:
star_message(0) star_message(1) star_message(2)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!