must be in Python please Instructions for full credit always use explanatory com
ID: 3599952 • Letter: M
Question
must be in Python please
Instructions
for full credit always use explanatory comments
Task: Revise the simple Guessing Game from Assessment 2 (chap 3 &4)
Part I Use While Loop
file Name: yourlastname5py
Rules: The computer generates a random number between 1 and 10
The player guesses until they guess correctly
HINT: use a While loop while the user's guess and the random (winning) number are not equal
If the numbers are equal, the player wins.
If the player's number is higher than the winning number
tell the player to guess lower
If the player's number is lower than the winning number
tell the player to guess higher
Otherwise, the computer wins
**Use a counter within the loop, to count how many times it took user to guess the winning number
Output Examples:
Guess on 1st try
Welcome to Guess a Number Game
Let's see how many guesses it takes....
Guess a number between 1 and 10
Wow, you won!
It took you 1 guess(es)
Congratulations!
Game over. goodbye
Another Output example (multiple tries):
Welcome to Guess a Number Game
Let's see how many guesses it takes....
Guess a number between 1 and 10: 2
guess is low
Guess again: 8
guess is high
Guess again: 6
Wow, you won!
It took you 3 guess(es)
Congratulations!
Game over ~ goodbye
Once you get that working, you will nest your code within another while loop
The outer loop will determine if the user wants to play again (y or n)
HINT: Don't forget to take into consideration that a user may enter "Y" or "y".
You may declare the boolean variable by using a String or char data type
Part II Nested Loop
Example Output:
Welcome to Guess a Number Game
Let's see how many guesses it takes....
Enter an integer from 1 to 10: 9
guess is high
Guess again: 1
guess is low
Guess again: 2
Wow you win!
Congratulations!
It took you 2 guess(es)
Would you like to play again? (y or n)
y
Enter an integer from 1 to 10: 5
guess is high
Guess again: 1
guess is low
Guess again: 2
guess is low
Guess again: 4
Wow you win!
Congratulations!
It took you 4 guess(es)
Would you like to play again? (y or n)
n
Game over ~ Goodbye
Explanation / Answer
Part1:
1)code work fine.when testing in your system take care of intendation when you are copying the code into your system
2)i have written for you specified two parts
3)if you find any issue please drop a comment
4)if your are satisfied please provide a positive feedback by giving thumbsup
#!/usr/bin/python
import random
print('Welcome to Guess a Number Game ') #to print message
print('Lets see how many guesses it takes....') #to print message
ans=99;#assigning some default value
total=0;#intializing to zero it is used for tracking number of guesses
question=0;#assigning some initial value
n=random.random();#to get the random number we are using random function.it returns value between 0 to 1.0
k=n*10;#making the value fall between 1 to 10
question=int(k) #coverting float value to int
ans=int(ans) #coverting float value to int
print('Guess a number between 1 and 10')#to print message
while(question!=ans): #repeat until the correct guess found by the user
total=total+1;#to count the guesses
a=input('') #to read answer
ans=int(a);#converting the string to int
if ans<question: #to check guess is less than the required
print('Guess is low')
print('Guess again:')
elif ans>question: #to check guess is greaterthan required
print('Guess is high')
print('Guess again:')
elif ans==question: #to check guess is equal to the question
print('Wow you win!')
print('Congratulations!')
print('It took you',total)
print('Guess(es)')
print('Game Over ~ Goodbye')
else:#if nothing matches
print('')
part2:
#!/usr/bin/python
import random
print('Welcome to Guess a Number Game ') #to print message
print('Lets see how many guesses it takes....') #to print message
choice='y';
while(choice!='n'):#to repeat the asking for playing again until user enters n
ans=99;#assigning some default value
total=0;#intializing to zero it is used for tracking number of guesses
question=0;#assigning some initial value
n=random.random();#to get the random number we are using random function.it returns value between 0 to 1.0
k=n*10;#making the value fall between 1 to 10
question=int(k) #coverting float value to int
ans=int(ans) #coverting float value to int
print('Guess a number between 1 and 10')#to print message
while(question!=ans): #repeat until the correct guess found by the user
total=total+1;#to count the guesses
a=input('') #to read answer
ans=int(a);#converting the string to int
if ans<question: #to check guess is less than the required
print('Guess is low')
print('Guess again:')
elif ans>question: #to check guess is greaterthan required
print('Guess is high')
print('Guess again:')
elif ans==question: #to check guess is equal to the question
print('Wow you win!')
print('Congratulations!')
print('It took you',total)
print('Guess(es)')
else:#if nothing matches
print('')
choice=input('would you like to play again?(y or n)')#to take the choice
print('Game Over ~ Goodbye')
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.