Python Forum
Having trouble with my Computer Science task
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Having trouble with my Computer Science task
#1
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")
Yoriz write Oct-07-2021, 11:10 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Attached Files

Thumbnail(s)
   
Reply
#2
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)
Dunxx likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Computer science can you help me with the last part of the code after mentionedWords. shirleylam852 9 3,967 Nov-28-2020, 01:29 AM
Last Post: shirleylam852
  computer science coursework, read the text please and tell me if theres any specifics sixcray 4 2,557 Nov-11-2020, 03:17 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020