In a modify task you take code that already works and adapt it so that it does something else.
The editor starts you off with a guessing game that is not much of a game yet:
secret_number = 7
print("Hello, what's your name?")
player_name = input()
print("Okay " + player_name + ", guess a number between 1 and 10:")
guess = int(input())
if guess < secret_number:
print("Your guess is too low")
elif guess > secret_number:
print("Your guess is too high")
else:
print("Congratulations! You guessed correctly.")
Run it first. The secret number is 7 on every run, so once you have played it once, you can win it every time.
Change the program so that:
Some details, so that your game can be checked automatically:
print, exactly as
written below, and read the answer with a bare input(). The user always
answers easy or hard, in lower case.random.randint, once, after the user
has chosen the mode. Do not generate a number for both modes and then pick
one. The starter code has no import random yet, so add it at the top.between 1 and 10 in
easy mode, between 1 and 100 in hard mode.The two questions come first, in this order:
Hello, what's your name?
Which mode do you want to play, easy or hard?
Then the guess question, where NAME is the name the user typed and HIGHEST
is 10 or 100:
Okay NAME, guess a number between 1 and HIGHEST:
And finally one of the three messages the program already prints.
The secret number is random, so your program will not always give the output shown here. These examples show what happens when the secret number is 6 in the first run and 49 in the second.
Input:
Priya
easy
3
Output:
Hello, what's your name?
Which mode do you want to play, easy or hard?
Okay Priya, guess a number between 1 and 10:
Your guess is too low
Input:
Mo
hard
49
Output:
Hello, what's your name?
Which mode do you want to play, easy or hard?
Okay Mo, guess a number between 1 and 100:
Congratulations! You guessed correctly.
How can a random program be checked?
A computer cannot really pick numbers at random. It works them out from a starting value called a seed, and the same seed always gives the same numbers. The checks for this exercise fix the seed before your program runs, so they know which secret number your program is going to pick, and can choose guesses that are too low, too high or exactly right.
That only works if your program asks for the same random number the checks
expect, which is why the task says to call random.randint once, after the
mode is known. When you run the program yourself the seed is not fixed, and you
get a different secret number every time.
Ask each question with its own print, and read the answer with a bare
input() on the next line, rather than passing the question to input(...).
The questions are part of the output that is checked here, so their spelling,
capital letters and punctuation count.
Make sure that you check for the following things:
import random is the first line of the program.== against "easy" or "hard", in speech marks
and in lower case.random.randint includes both of its numbers, so random.randint(1, 10) can
pick the 10.str() if it is stored as a number.