Andrew Andrew - 4 months ago 11
Python Question

Game code error line skipping - Python

I have this code for a number guessing game and i was playing it when I noticed a problem.

# Guessing the number

print("I'm thinking of a number between 1 and 20. Can you guess my number?")

r = str(input('{} please enter a integer between 1 and 10: '.format(name)))

r = str(input('{} please enter a integer between 1 and 10: '.format(name2)))

print (name, ' you chose ', r)
print (name2, ' you chose ', p)
r1 = random.randrange(10)


Printing who won



if r == p:
print('Plz choose different numbers from each other')
if r == r1:
print('Computer chose', r1,',',name,' Wins!')
if p == r1:
print('Computer chose', r1,',',name2,' Wins!')
elif (r > 'r1' and p < 'r1'):
print('Computer chose', r1, ' both of you lose')
elif (r < 'r1' and p < 'r1'):
print('Computer chose', r1, ' both of you lose')
elif (r > 'r1' and p > 'r1'):
print('Computer chose', r1, ' both of you lose')
elif (r < 'r1' and p > 'r1'):
print('Computer chose', r1, ' both of you lose')


When the computer read this part of the code

if r == p:
print('Plz choose different numbers from each other')
if r == r1:
print('Computer chose', r1,',',name,' Wins!')
if p == r1:
print('Computer chose', r1,',',name2,' Wins!')
elif (r > 'r1' and p < 'r1'):
print('Computer chose', r1, ' both of you lose')
elif (r < 'r1' and p < 'r1'):
print('Computer chose', r1, ' both of you lose')
elif (r > 'r1' and p > 'r1'):
print('Computer chose', r1, ' both of you lose')
elif (r < 'r1' and p > 'r1'):
print('Computer chose', r1, ' both of you lose')


It skipped the first parts and keep saying that both players lost. Why is this happening and how can I fix it?

Answer

I think the problem is here:

r = str(input('{} please enter a integer between 1 and 10: '.format(name)))

r = str(input('{} please enter a integer between 1 and 10: '.format(name2)))

Don't you mean to assign to p the second time through? p = str(...

Also, these lines don't make much sense:

elif (r > 'r1' and p < 'r1'):
    print('Computer chose', r1, ' both of you loose')
elif (r < 'r1' and p < 'r1'):
    print('Computer chose', r1, ' both of you loose')
elif (r > 'r1' and p > 'r1'):
    print('Computer chose', r1, ' both of you loose')
elif (r < 'r1' and p > 'r1'):
    print('Computer chose', r1, ' both of you loose')

You're comparing r and p to the string 'r1'. You probably mean to be comparing to r1 itself, but even then the logic is really strange. I think you just want if r != r1 and p != r1, but the below is much simpler:

if r == p:
    print('Plz choose different numbers from each other')
elif r == r1:
    print('Computer chose', r1,',',name,' Wins!')
elif p == r1:
    print('Computer chose', r1,',',name2,' Wins!')
else:
    print('Computer chose', r1, ' both of you loose')

(Note, though, that the word is spelled "lose.")

UPDATE

You probably want integers:

r = int(input('{} please enter a integer between 1 and 10: '.format(name)))

r = int(input('{} please enter a integer between 1 and 10: '.format(name2)))
Comments