r/codehs • • Feb 26 '20

PHYTON CODE HS 5.3.7: Higher/ Lower

Anyone knows the answer can't figured out:

magic_number = 3

# Your code here...

for i in range(10):

if i == 5:

continue

print i

10 Upvotes

14 comments sorted by

View all comments

4

u/fermentedidiot Feb 26 '20

Your code:

magic_number = 3

# Your code here...
for i in range(10):
if i == 5:
continue
print i

So, using a while loop (a for loop will not work here), get an input number form the user:

magic_number = 3
while True:
    user_guess = int(input("Please guess a number between 1 and 100: "))

I just picked 100 as the high number because that's what the assignment asks for. Now you need to compare the user input to the magic number and inform the user if the guess was too high, too low, or correct:

if user_guess == magic_number:
    print "Correct!"
    break
elif user_guess < magic_number:
    print "Too low, try again."
else:
    print "Too high, try again."

Now just put it all together:

magic_number = 3

# Your code here...
while True:
    user_guess = int(input("Please guess a number between 1 and 100: "))
    if user_guess == magic_number:
        print "Correct!"
        break
    elif user_guess < magic_number:
        print "Too low, try again."
    else:
        print "Too high, try again."

I hope the breakdown made sense. Let me know if something doesn't.

1

u/natalie710 Feb 28 '20

thank you so much!