Question

PYTHON Stuck on this problem: Write a program for playing a game with the computer. First,...

PYTHON

Stuck on this problem:

Write a program for playing a game with the computer. First, generate a random integer in the range of 6 through 9. This will be the computer’s pick. Then generate three random integers for the human player. These random integers are in the range of 1 through 10. If any of these three random integers is greater than the computer’s pick, the human player wins the game. Otherwise, the human player loses. You must use a for loop to generate these three random integers. In each iteration, generate and display one random integer. If it is larger than the computer’s pick, display “You have won the game” and break out of the loop. If the human player cannot get a random integer larger than computer’s pick after three iterations , display “sorry you have lost the game”. The following is an example: Computer's pick: 9 Your pick: 8 Your pick: 3 Your pick: 2 Sorry, you have lost the game Another example: Computer's pick: 6 Your pick: 5 Your pick: 8 You have won the game ***Cannot use "flag" because we have not covered this yet in class.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#To generate random numbers we need to import random class
import random as r
computer=r.randint(6,9)#Generating random for computer.

print("Computer's Pick : ",computer)#printing the Computer's Pick.

#for loop to generate the random numbers for human user.
for i in range(3):

human=r.randint(1,10)#random number generating for human

print("Human's pick "+str(i)+":",human)#printing Human's Pick

if(human > computer):#condition to check whether human's pick is greater than Computer's Pick
print("You have won the game")
break #breaks only if humans pick is greater than computer's pick.

#if the loop is not broke that means all the human's picks are lesser than computer's pick

#if the loop breaks then the recent human value is greater than Computer's pick.

if(human <= computer):
print("sorry you have lost the game")

OUTPUT:-

Add a comment
Know the answer?
Add Answer to:
PYTHON Stuck on this problem: Write a program for playing a game with the computer. First,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PYTHON Write a program for playing a game with the computer. First, generate a random integer...

    PYTHON Write a program for playing a game with the computer. First, generate a random integer in the range of 6 through 9. This will be the computer’s pick. Then generate three random integers for the human player. These random integers are in the range of 1 through 10. If any of these three random integers is greater than the computer’s pick, the human player wins the game. Otherwise, the human player loses. You must use a for loop to...

  • Write a C# program that allows one user to play Rock-Paper-Scissors with computer. The user will...

    Write a C# program that allows one user to play Rock-Paper-Scissors with computer. The user will pick a move (Rock Paper Scissors) from 3 radio buttons and click the play button to play the game. The application will use random number generator to pick a move for the computer. Then, the application display the computer's move and also display whether you won or lost the game, or the game is tie if both moves are the same. The application must...

  • War—A Card game Playing cards are used in many computer games, including versions of such classic...

    War—A Card game Playing cards are used in many computer games, including versions of such classics as solitaire, hearts, and poker. War:    Deal two Cards—one for the computer and one for the player—and determine the higher card, then display a message indicating whether the cards are equal, the computer won, or the player won. (Playing cards are considered equal when they have the same value, no matter what their suit is.) For this game, assume the Ace (value 1) is...

  • Python Program Python: Number Guessing Game Write a Python function called "Guess.py" to create a number...

    Python Program Python: Number Guessing Game Write a Python function called "Guess.py" to create a number guessing game: 1. Function has one input for the number to Guess and one output of the number of attempts needed to guess the value (assume input number will always be between 1 and 1000). 2. If no number was given when the function was called, use random.randint(a,b) to generate a random number between a=1 and b=1000. You will also need to add an...

  • **IN JAVA** Make a Game -Game Strategy The program and the user alternate turns, picking up...

    **IN JAVA** Make a Game -Game Strategy The program and the user alternate turns, picking up one, two, or three straws on each turn. The program goes first. The player to pick up the last straw loses. -Game process Your program should start by generating and displaying a random integer (which represents the number of straws) between 10 and 20 inclusive. If this number is 1 more than a multiple of 4, add 1. For example, if the random integer...

  • It's writing a simple rock paper scissors game strictly following the instructions. INSTRUCTIONS: If the user...

    It's writing a simple rock paper scissors game strictly following the instructions. INSTRUCTIONS: If the user selects 'p': 1. First the program should call a function named getComputerChoice to get the computer's choice in the game. The getComputerChoice function should generate a random number between 1 and 3. If the random number is 1 the computer has chosen Rock, if the random number is 2 the user has chosen Paper, and if the random number is 3 the computer has...

  • In C... Write a program to simulate a pick-5 lottery game. Your program must generate and...

    In C... Write a program to simulate a pick-5 lottery game. Your program must generate and store 5 distinct random numbers between 1 and 9 (inclusive) in an array. The program prompts the user for: an integer random seed five distinct integers between 1 and 9 (which are stored in another array) The program then compares the two arrays to determine if they are identical. If the two arrays are identical, then the user wins the game. otherwise the program...

  • Has to be written in C++. Visual studio. Write a C++ program to realize the game...

    Has to be written in C++. Visual studio. Write a C++ program to realize the game of guessing the number. Generally, the game will generate a random number and the player has to find out the number. In each guess, the program will give you a feedback as your guess is correct, too large, or too small. Then the player play repetitively until find out the number. Specifically, the game plays as the following. a). When the game is started,...

  • Write a C++ program that simulates playing the Powerball game. The program will generate random numbers...

    Write a C++ program that simulates playing the Powerball game. The program will generate random numbers to simulate the lottery terminal generated numbers for white balls and red Powerball. The user will have the option to self-pick the numbers for the balls or let the computer randomly generate them. Set the Grand Prize to $1,000,000,000.00 in the program. Project Specifications Input for this project: Game mode choice – self pick or auto pick Five numbers between 1 and 69 for...

  • Write a Python program (using python 3.7.2) that lets the user play the game of Rock,...

    Write a Python program (using python 3.7.2) that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. 1. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. (Don’t display the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT