-1
#test
import subprocess
import time
import random
programClass=[]
code=random.randint(100,1000)
print('This is your secret code! PLEASE WRITE THIS DOWN!')
print(code)
numRafflePeople=0

tester=1
while tester==1:
    code1=input('What is your name, phone number, and email?\n')
    print('code',code1)
    code=code
    if code==code1:
        print('Blah')
        time.sleep(5)
        tester=2
    else:
        print('fail')
        tester=1

This program makes a random number, then checks to see if the inputed number is the same as the random number, however when I run the program the program does not seem to be able to identify they are the same.

The random number will be like 303. I will type 303, and the fail message will be printed, can someone please explain the error in my code?

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Please try to create a [mcve]. The while loop is not necessary for the issue here – OneCricketeer Nov 30 '17 at 03:57
  • 1
    Possible duplicate of [How can I read inputs as integers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-integers) – OneCricketeer Nov 30 '17 at 03:58
  • Its fine, I got the answer I desired, the while loop is necessary for my bigger project, but it seemed to big – QuestionedE Nov 30 '17 at 03:59
  • My point is that you should post a smaller example that reproduces the issue. Basically, you're comparing a string to an int – OneCricketeer Nov 30 '17 at 04:00
  • Thats what I'm trying to do in this certain example, thanks though ill remember this for the future! You have been a great help towards asking relevant questions! – QuestionedE Nov 30 '17 at 04:02

2 Answers2

3

When the user gives input, code1 becomes a string. code is an integer.

When comparing the two in the boolean, code==code1, it will always be false.

Just change it to:

code1=int(input('What is your name, phone number, and email?\n'))
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
1

You must convert your input to int:

import time
import random

code = random.randint(100,1000)
print('This is your secret code! PLEASE WRITE THIS DOWN!')
print(code)

tester = 1
while tester == 1:
    code1 = int(input('What is your name, phone number, and email?\n'))
    print('code', code1)
    if code == code1:
        print('Blah')
        time.sleep(5)
        tester = 2
    else:
        print('fail')
        tester = 1
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80