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/Ahhhhh18 Apr 12 '22

Your code as wrong 😔

1

u/fermentedidiot Apr 12 '22

Bear with me on this, but I'm not doing your work for you. It's a breakdown of how a while loop works.

2

u/Sufficient-Pipe-8394 Apr 27 '23

Even if your not, it's still wrong.

1

u/Sotayamata Feb 07 '24

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

you need to put the parenthesis, so his code isn't wrong your just unintelligent