Drop hier links of afbeeldingen om ze aan de editor toe te voegen.

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.

Your task

Change the program so that:

  1. The secret number is generated randomly.
  2. The game has an easy mode and a hard mode, and the user is asked which one they want to play.
  3. Easy mode generates a number from 1 to 10, and hard mode generates a number from 1 to 100.

Some details, so that your game can be checked automatically:

The output

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.

Example

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.

Help! My code doesn't work!

Make sure that you check for the following things:

  • import random is the first line of the program.
  • The mode is compared with == 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.
  • The secret number is generated once, after the mode question has been answered.
  • The guess question shows the highest number for the mode that was chosen, wrapped in str() if it is stored as a number.