Question

Can someone please rewrite this code using a while loop? Please write it in python. #The...

Can someone please rewrite this code using a while loop? Please write it in python.

#The winning number my_number = 12

#variable for the input name

user_n = input("Hello! What is your name?")

#Ask the user

print("Well",user_n,": I am thinking of a number between 1 and 20." + "Take a guess.") user_n = float(input("Take a guess."))

#if statements

if user_n == my_number: print("Good job" ,user_n, "You guessed my number!")

if user_n > my_number: print("Your guess is high. You lose.")

if user_n < my_number: print("Your number is to low. You lose the game.")

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

#The winning number
my_number = 12
#variable for the input name
user_n = input("Hello! What is your name?")
#Ask the user
print("Well",user_n,": I am thinking of a number between 1 and 20." + "Take a guess.")
user_n = float(input("Take a guess."))
#while statements
while user_n != my_number:
if user_n > my_number:
print("Your guess is high. You lose.")
if user_n < my_number:
print("Your number is to low. You lose the game.")
user_n = float(input("Take a guess again."))
print("Good job" ,user_n, "You guessed my number!")

Output:

$python3 main.py
Hello! What is your name?Suresh 
Well Suresh
 : I am thinking of a number between 1 and 20.Take a guess. 
Take a guess. 8
Your number is to low. You lose the game.
Take a guess again.15
Your guess is high. You lose.
Take a guess again.12
Good job 12.0 You guessed my number!

Add a comment
Know the answer?
Add Answer to:
Can someone please rewrite this code using a while loop? Please write it in python. #The...
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
  • on python i need to code a guessing game. After the player has guessed the random...

    on python i need to code a guessing game. After the player has guessed the random number correctly, prompt the user to enter their name. Record the names in a list along with how many tries it took them to reach the unknown number. Record the score for each name in another list. When the game is quit, show who won with the least amount of tries. this is what i have so far: #Guess The Number HW assignment import...

  • In Java You’re going to make a Guess the Number game. The computer will "think" of...

    In Java You’re going to make a Guess the Number game. The computer will "think" of a secret number from 1 to 20 and ask the user to guess it. After each guess, the computer will tell the user whether the number is too high or too low. The user wins if they can guess the number within six tries. The program should look like this in the console, player input is in bold: Hello! What is your name? Abaddon...

  • Can you add code comments where required throughout this Python Program, Guess My Number Program, and...

    Can you add code comments where required throughout this Python Program, Guess My Number Program, and describe this program as if you were presenting it to the class. What it does and everything step by step. import random def menu(): #function for getting the user input on what he wants to do print("\n\n1. You guess the number\n2. You type a number and see if the computer can guess it\n3. Exit") while True: #using try-except for the choice will handle the...

  • Can you please enter this python program code into an IPO Chart? import random def menu():...

    Can you please enter this python program code into an IPO Chart? import random def menu(): print("\n\n1. You guess the number\n2. You type a number and see if the computer can guess it\n3. Exit") while True: #using try-except for the choice will handle the non-numbers #if user enters letters, then except will show the message, Numbers only! try: c=int(input("Enter your choice: ")) if(c>=1 and c<=3): return c else: print("Enter number between 1 and 3 inclusive.") except: #print exception print("Numbers Only!")...

  • how would i write my code in Pseudocode? I am pretty much a beginner, therefore this...

    how would i write my code in Pseudocode? I am pretty much a beginner, therefore this is my first major computer science assignment and im not really understanding the written way of code.. please help and explain. Thank you! this is my working code: # Guess The Number HW assignment import random guessesTaken = 0 names=[] tries=[] print("Welcome! ") question = input("Would you like to play the guessing game? [Y/N]") if question == "N": print("Sorry to see you go so...

  • Hello! we are using Python to write this program. we are supposed to use loops in...

    Hello! we are using Python to write this program. we are supposed to use loops in this assignment. I would greatly appreciate the help! Thank you! Write a program that allows the user to play a guessing game. The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: we would have the program select a random number as the "secret number". However, for the purpose of testing...

  • A simple game of chance Using a while loop write a simple coin flip game. Specifics:...

    A simple game of chance Using a while loop write a simple coin flip game. Specifics: The user starts with a bank of $10.00 It costs a dollar to play A correct guess pays $2.00 The while loop is used for the game loop. It should ask if the user wants to play. The while loop will check for a value of 'Y' or 'N'. You should also allow for lower case letters to be input. You MUST prime the...

  • Can someone upload a picture of the code in matlab Write a "Guess My Number Game"...

    Can someone upload a picture of the code in matlab Write a "Guess My Number Game" program. The program generates a random integer in a specified range, and the user (the player) has to guess the number. The program allows the use to play as many times as he/she would like; at the conclusion of each game, the program asks whether the player wants to play again The basic algorithm is: 1. The program starts by printing instructions on the...

  • This is python code I am having trouble with the both winning messages printing if the...

    This is python code I am having trouble with the both winning messages printing if the user guesses in 1 try. 1 message or the other should print depending on the number of guesses not both. Thank you for your help! import random answer = 5 tries = 0 user_guess = "" while True: user_guess = int(input("enter a number between 1 and 10: ")) tries = tries + 1    if user_guess == answer: print ("You win! \nIt took you",...

  • 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...

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