Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Programming Problem
#1
So I'm kinda stuck on this one program I am trying to write and idk what is wrong. The question says to write a program to guess the computer's randomly chosen number until you guess it correctly. This is a sample of what the code should produce:

Output:
Guessing game Instructions: Try to guess the number between 1 and 100. Enter number (1 to 100): 50 Too low! Enter number (1 to 100): 100 Too high! Enter number (1 to 100): 75 Too low! Enter number (1 to 100): 90 Too high! Enter number (1 to 100): 85 Too high! Enter number (1 to 100): 80 Too high! Enter number (1 to 100): 78 Too low! Enter number (1 to 100): 79 That's correct! It took you 8 tries to correctly guess 79
And... This is the code I have so far but something isn't right about it:

print("Guessing game")
print("\nInstructions:\tTry to guess the number between 1 and 100.")
import random
number = random.randint(1,100)
number2 = int(input("\nEnter number (1 to 100): "))
while number2 < number:
    guess_number = int(input("Too low! Enter number (1 to 100): "))
while number2 > number:
    guess_number = int(input("Too high! Enter number (1 to 100): "))
if number == guess_number:
    print("That's correct!")
Any help would be great! Thanks!
Reply
#2
you need to repeat what lines 5-11 do as long as the guess is incorrect. Use while loop.
line 6 and line 8 - you want to use if like you do in line 10, not while, or you can combine 6, 8 and 10 in a single if/elif block.
Reply
#3
print("Guessing game")
print("\nInstructions:\tTry to guess the number between 1 and 100.")
import random
number = random.randint(1,100)
number_of_tries = 0
guess_number = int(input("\nEnter number (1 to 100): "))
too_low = int(input("Too low! Enter number (1 to 100): "))
too_high = int(input("Too high! Enter number (1 to 100): "))
while guess_number == int(input("\nEnter number (1 to 100): ")):
    if guess_number < number:
        print(too_low)
    if guess_number > number:
        print(too_high)
    if guess_number == number:
        print("That's correct!")
number_of_tries += 1

Like this?
Reply
#4
sort of...
does it do what you expect it to do? if not - what is not right?
Reply
#5
Not quite and I'm not sure..
Reply
#6
import random

print("Guessing game")
print("\nInstructions:\tTry to guess the number between 1 and 100.")
number = random.randint(1,100)
number_of_tries = 0
while True:
    guess_number = int(input("\nEnter number (1 to 100): "))
    number_of_tries += 1
    if guess_number < number:
        print('Too_low')
    elif guess_number > number:
        print('Too_high')
    else:
        print("That's correct!")
        break
Reply
#7
That's still not right.. It is supposed to print out the same as this except different numbers of course:

Guessing game

Instructions: Try to guess the number between 1 and 100.

Enter number (1 to 100): 50
Too low! Enter number (1 to 100): 100
Too high! Enter number (1 to 100): 75
Too low! Enter number (1 to 100): 90
Too high! Enter number (1 to 100): 85
Too high! Enter number (1 to 100): 80
Too high! Enter number (1 to 100): 78
Too low! Enter number (1 to 100): 79

That's correct!

It took you 8 tries to correctly guess 79

It's almost right not quite though.
Reply
#8
well, I'm not going to do it for you 100% percent :-)
I wanted to show you how to make the loop the right way. Hint - print function takes an extra argument end
Reply
#9
Figured it out. Thanks.

.....
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Verilog HDL Programming to Python Programming? noobcoder 1 3,131 Jul-18-2019, 09:28 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020