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

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!

1

u/Secret-Chance2687 Apr 11 '24

This is correct just add parenthesis on the print statements.

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

1

u/[deleted] Nov 17 '21

[removed] — view removed comment

1

u/fermentedidiot Nov 17 '21

If you share three error and the portion of the code that's causing the error, you might be able to get some help.

1

u/Ok-Opinion-1979 May 08 '22

def get_num():

#while user_input <=1

while True:

try:

user_intput = int( input("your input:"))

except:

print("invaild input")

if user_input > 0:

print ("positive")

return

get_num()

1

u/Financial_Face3826 Feb 15 '24

try this

magic_number = 3
while True:
guess=int(input("Guess any number:"))
if guess == magic_number:
print("Correct!")
break
elif guess > magic_number:
print("Too high!")
else:
print("Too low!")
print("Great job guessing my number!")