Python Forum
Programming Problem - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Programming Problem (/thread-7927.html)



Programming Problem - ShakenBake - Jan-30-2018

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!


RE: Programming Problem - buran - Jan-30-2018

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.


RE: Programming Problem - ShakenBake - Jan-30-2018

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?


RE: Programming Problem - buran - Jan-30-2018

sort of...
does it do what you expect it to do? if not - what is not right?


RE: Programming Problem - ShakenBake - Jan-30-2018

Not quite and I'm not sure..


RE: Programming Problem - buran - Jan-30-2018

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



RE: Programming Problem - ShakenBake - Jan-30-2018

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.


RE: Programming Problem - buran - Jan-31-2018

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


RE: Programming Problem - ShakenBake - Jan-31-2018

Figured it out. Thanks.

.....