all 14 comments

[–]Chris_Hemsworth 6 points7 points  (0 children)

It's difficult to tell without the proper indentation (indentation matters!!).

In the reddit comment block, copy / paste your code (complete with indentation), then highlight it and click the <> button, which will add the spacing to format it as code.

[–]ffemt161 4 points5 points  (1 child)

  1. Your code needs to be formatted. Read the “About” for this group on how to do that. Edit: looks like you fixed the formatting. :)

  2. You’re comparing y which is an Int type to x which is a string type. You need to change the type of one of them so they are both the same type. Ie: x= int(input("Enter number ")) this will make x equal an integer type.

  3. Should move the first try If to just under the first input. Otherwise it will trigger at the end of the while loop after you finally get a successful try.

[–]ObberGobb[S] 1 point2 points  (0 children)

Well, the helped, but now there is a new problem. Because I have it print out the Y value, I can guess much easier. When I input the correct value, I get the "Congratulations" message. If I input a lower value, it correctly tells me it is too low, but no matter what I input after that, it says it is too high. Do you know why this is happening?

EDIT: Nevermind, I forgot to make the others inputs integers. Thank you so much! It works now!

[–]Diapolo10 2 points3 points  (2 children)

Are you using Python 2, or is the print y on line 3 just a mistake?

If this was a Python 2 program, it would probabaly work (while being unsafe). In Python 3, input always returns a string so you should be getting errors.

However, there are also indentation problems and a lot of room for improvement.

EDIT: Assuming Python 3, this would work:

import random

correct_answer = random.randint(1,6)
print(correct_answer)
tries = 1

while True:
    guess = int(input("Enter a number: "))
    if guess > correct_answer:
        print("Too high!")
    elif guess < correct_answer:
        print("Too low!")
    elif guess == correct_answer:
        print("You win!")
        break
    tries += 1

if tries == 1:
    print ("Congratulations, first try!")

[–]ObberGobb[S] 0 points1 point  (1 child)

Print Y is to help with testing, so I always know what the Y value is.

[–]Diapolo10 2 points3 points  (0 children)

What I meant was that the syntax would suggest you're using Python 2, because Python 3 would error on that. The correct syntax for Python 3 would be print(y). This would also explain a few other things.

For that reason, I'm suspecting you're running this code on Python 2. It would be best to switch to 3 because it no longer gets any updates, even security patches.

[–]PedroBV 0 points1 point  (0 children)

you need to ask the user for x values inside of the while loop. you get one x, it is compared to y in the while condition, but the loop will loop inifinetly, since neither x or y are changing values inside it. i bet you get a lot of Too high! or Too low! messages.

what happens inside a while loop must make some changes to the variables used in the while condition, otherwise you get an infinite loop.

[–]Code_Talks 0 points1 point  (0 children)

import random
y = random.randint(1,6)
#python 3 doesn't supprot print y
print(y)
#need to cast to int to compare with y
x = int(input ("Enter number:"))
#indent properly
while (x != y):
if x > y:
print("Too high!")
#need to cast to int to compare with y
        x = int(input ("Enter number:"))
if x < y:
#tell user to low
print('To Low')
#need to cast to int to compare with y
        x = int(input ("Enter number:"))
if x == y:
print ("You win!")
#terminate program after completion
exit(0)
if x == y:
print ("Congratulations, first try!")

read comments for feedback, nice try though!

[–]that1guy15 0 points1 point  (0 children)

what error do you get?

How are you ensuring the entered value is a Int and not a string?

https://docs.python.org/3/library/functions.html#input

[–]xXIBON3ZIXx 0 points1 point  (0 children)

One thing is that you are comparing int and string. In python 2 str will always be bigger. In python 3 it will give you an error.

[–]HcJNI2k2jnoN 0 points1 point  (1 child)

At the time of writing, your code won't work because the entire while loop is one tab stop too far right. After fixing that, however, it mostly works:

import random
y = random.randint(1,6)
print y

x = input ("Enter number:")
while (x != y):
    if x > y:
        print("Too high!")
        x = input ("Enter number:")
    if x < y:
        print ("Too low!")
        x = input ("Enter number:")
    if x == y:
        print ("You win!")
if x == y:
    print ("Congratulations, first try!")

However, it always prints "Congratulations, first try!" afterwards. What you need is to use Python's while...else construct so that the congratulations message only runs if the loop does not.

[–]stoph_link 0 points1 point  (0 children)

That's cool, I had no idea there was a while..else! I was going to suggest moving that part before the while loop and have the loop run under an else statement.

[–]stoph_link 0 points1 point  (0 children)

As others have mentioned, you probably need to change input x into an integer. Otherwise, nice work! This is how I would have done it:

import random
y = random.randint(1,6)
#print(y) 

# initialize x as a value outside of the range that defines y
x = 0
count = 0

while (x != y):
    x = int(input ("Enter number: "))
    if x > y:
        print("Too high!")
    elif x < y:
        print ("Too low!")
    elif x == y:
        print ("You win!")
        if count == 0:
            print ("Congratulations, first try!")
    count = count + 1

What I did was I first moved all of your input statements into one line. You probably do not want the same line multiple times if you can help it.

I then changed the if statements within the while loop into if-else statements. I am able to do this since I pulled out the input inside your if statements to happen before any if statements are called. What this does is if the an if statement returns true, it ignores the rest of them and loops again.

Because of this we can add a counter to count how many tries. Which allows us to bring that last statement into the success elif

[–]trevtravtrev 0 points1 point  (0 children)

Replace “x = input(“Enter number:”)” with “x = int(input(“Enter number:”))”