Having trouble with my Computer Science task - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Having trouble with my Computer Science task (/thread-35180.html) |
Having trouble with my Computer Science task - Dunxx - Oct-07-2021 My computer science teacher doesn't know python, ive spent 20 minutes trying to figure this out but cannot. This is my task You are required to produce a simple guess the Number Game Program should generate an integer within the range of 1 to 10 Player should guess the number If the guess is wrong, your program should tell the player if the number guessed is smaller or greater than the number. This should continue until the player makes a correct guess. Finally, it awards the player with five stars if he/she gets it right the time, four stars if he/she gets it right the second time and three stars if he/she gets it right the third time. Make it into Python code. What I've done. count=0 import random i=random.randint(1,11) while True: guess=input(int("Guess the number that was generated from 1-10")) if guess==i: while False: elif i<guess: print("The number is smaller") count=count+1 elif: print("The number is greater") count=count+1 if count==1: print("You got 4 stars") elif count>=2: print("You got 3 stars") else count==0: print("you got 5 stars") RE: Having trouble with my Computer Science task - DeaD_EyE - Oct-07-2021 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) |