211k views
1 vote
Purpose:

Solidify and demonstrate your understanding and application of the conditional and looping programming constructs in Python by creating a number guessing game. Your program will use a random number generator to choose a mystery number between 1 and 10 and give the user 3 attempts to guess the mystery number.

Skills
The purpose of this assignment is to demonstrate use of multiple and nested loops and if statements. These are basic foundational coding skills that are widely used all programs regardless of the language or the problem the code is solving. In addition you will need to bring in a library to generate random numbers. Familiarity with using additional libraries also is needed skill for most python programs.

Knowledge
This assignment will help you become familiar with:

Using the random number library
Using Boolean variables to control looping
Using integer variables to repeat an action a fixed amount of times (e.g. only allowing user to have 3 guesses)
Using relatively complex conditional statements embedded inside while loops

User CaseyWebb
by
4.7k points

1 Answer

3 votes

Answer:

import random

mysteryNumber = random.randint(1,10)

print("I am thinking of a number between 1 and 10.")

guess = int(input("What's the number? "))

if guess == mysteryNumber:

print("Yes! You win!")

else:

print("Nope, try again.")

guess = int(input("What's the number? "))

if guess == mysteryNumber:

print("Yes! You win!")

else:

print("Nope, try again.")

guess = int(input("What's the number? "))

if guess == mysteryNumber:

print("Yes! You win!")

else:

print("Nope, you lose. The number was",mysteryNumber)

User Superigno
by
5.8k points