0

here is my complete code if you need it: (the print contents are german)

import random
import time

n = 0
guess = 0
tries = 0

print("###Zahlen erraten###")
time.sleep(2)
print("")
time.sleep(1)


while True:
    n = input("Grenze eingeben: ")
    if n.isdigit():
        break

n = int(n)

Number = random.randint(0,n)
print(Number)
print(type(Number))

while True:
    guess = input("Zahl erraten: ")
    if guess.isdigit():
        guess = int(guess)
        print(type(guess))
        while guess == None or guess < 0:
            guess = input("Neue eingabe: ")
            if guess.isdigit():
                guess = int(guess)
                print(type(guess))


    if guess == Number:
        tries += 1
        break
    elif guess < Number:
        print("Die gesuchte Zahl ist größer!")
        tries += 1
        pass
    elif guess > Number:
        print("Die gesuchte Zahl ist kleiner")
        tries += 1
        pass


print("Gut gemacht! Die gesuchte Zahl ist "+ str(Number) + ". ")
print("Anzahl an versuchen: "+str(tries))

this is the error message if I type in a negative number or nothing in the variable "guess",

Traceback (most recent call last):
  File "C:\Users\micha\PycharmProjects\NumberGuesser\NumberGuesser.py", line 40, in <module>
    elif guess < Number:
TypeError: '<' not supported between instances of 'str' and 'int'

the problem is in line 40,elif guess < Number: .I would appreciate it if someone could help me resolve this problem.

  • I suggest you not to check if inputed value is digit, but try to convert it to int right after input. If it raises ValueError, you can handle it in except ValueError block. Now it's situation that you trying to compare guess (str type) with int. – Alpensin May 10 '22 at 16:01
  • Welcome to Stack Overflow. You should take a different approach to validating the input, as described in the linked duplicates; and you should use a function for the process of asking the user for an input, so that you can re-use that code instead of having to write it multiple times (every time you write it again is another chance to get it wrong). The immediate problem is a simple logical error: in the case where `if guess.isdigit():` isn't satisfied at line (if I counted correctly) 26, the rest of the code still runs with the original value of `guess` (which, of course, is a string). – Karl Knechtel May 10 '22 at 16:02

0 Answers0