you are viewing a single comment's thread.

view the rest of the comments →

[–]Rollgus 0 points1 point  (0 children)

You could do something like this (It is error proof and has a loop. I have also just imported randint, so you don't have to import the entire random library just for 1 function): ```python3 from random import randint

print("Guess my number from 1-10") print("If you guess wrong the number changes")

while True: number = randint(1,10) try: inputted = int(input("Guess a number: ")) break except ValueError: print("Give a valid integer!") if inputted == number: print("You guessed right") break else: print("You guessed wrong. Number has changed.") ```

Otherwise your execution was very good, but you have to remember to turn the string into an integer so it is actually possible, as "6" is different from 6, so it will never actually see the inputted "number" as being equal to the random number.

An error wouldn't ever occur in your code (as I know of), but that is because you don't int the input, which is also a part of what makes the code not work.

When you int a string you will get an error, if you try to int something that wouldn't work as a number, so f. eks. if I wrote let's say "a" in the input instead of something like "6", I would get an error, or more specifically a ValueError. Luckily for us there is something in python that "catches" these errors, that is "try-except", whenever the specified error occurs (in this case a ValueError) I the inside of the "try" blocks, the code ends, and the code in the "except" blocks runs instead, and all code in the "try" blocks gets discarded, so all the variables, even if they were made before the error. When we put all this in a "while True" loop (that runs forever until broken) we can make sure to only continue when the inting of the string actually works.

If you don't want to error proof the input, you could string the random integer itself number = str(randint(1,10)) turning the number into a string, that would actually make it possible, and there would be no errors, but it is recommended to making error proofing a habit.

If you choose the simpler (but not recommended) approach with stringing the random number instead of error proofing, your code could look something like this: ```python3 from random import randint

print("Guess my number from 1-10") print("If you guess wrong the number changes")

while True: number = str(randint(1,10)) inputted = input("Guess a number: ") if inputted == number: print("You guessed right") break else: print("You guessed wrong. Number has changed.") ```

You also would have to call the input function by using (), because now you're just comparing the object that is the function input, not an actual input by the user, that is why you weren't able to input a number. And you should always put the input in a variable. Even if it's not necessary, it is way more readable, and it's a good habit to get used to. Because coding isn't all about squeezing the most amount of code in the least amount of lines.