-1

I'm attempting to create a simple diced-base game where the user chooses a number, and if the program rolls it they win. The problem is that even when I win, the program says, "Oh no, you lose!" and I can't understand why. Help please!

    chosennumber = ("The dice rolled the number")
    user_text = "Choose a number between 1-6"
    print (user_text)
    number = input('Enter a number: ')
    import random
    mylist = [1,2,3,4,5,6]
    randomnum = random.choice(mylist)
    print (chosennumber, randomnum)
    if (number == randomnum):
        print ("You win")

    else:
        print ("Oh no, you lose!")
Zourtix
  • 11
  • 1
  • Your code is correct it seems , are you sure the number you gave and the random number that displays are same when you say they give the wrong output? – wolfsgang Dec 31 '15 at 20:45

1 Answers1

2

The problem is that "number" variable is a string, "input" always returns a string, and in your "if" you are comparing a string with a number which always will return false. You can check it using type(number).To solve this problem you have to convert the input to a integer using int(number).

Also you can make clearer your code. Put the import at the beginning and try not to create unnecessary variables. As mentioned you can avoid the list an use random to generate a number between 1 and 6

import random
chosennumber = ("The dice rolled the number")
print ("Choose a number between 1-6")
number = input('Enter a number: ')
randomnum = random.randint(1,6)
print (chosennumber, randomnum)
if (int(number) == randomnum):
    print ("You win")
else:
    print ("Oh no, you lose!")
Yábir Garcia
  • 349
  • 3
  • 17
  • Ah, that solved it, thanks. This was my first ever coding experience so I have learned a lot in making the project, and from your comment. :) – Zourtix Dec 31 '15 at 23:30