1

I was trying to code a simple program:

import random

x = raw_input("How many rounds:")
rounds = 0

while rounds < x:
    # rock=0, paper=1, scissors=2
    computer1 = random.randint(0,2)
    computer2 = random.randint(0,2)

    if computer1 == computer2:
        print "draw"
    elif computer1 == 0 and computer2 == 1:
        print "lose"
    elif computer1 == 1 and computer2 == 2:
        print "lose"
    elif computer1 == 2 and computer2 == 0:
        print "lose"
    else:
        print "win"
    rounds = rounds + 1

Why does this give me an infinite loop? When I take out the input line and replace x with a certain value, say, 10, the output does give me 10 results. But why can't I do it with that raw_input?

SuperBiasedMan
  • 9,814
  • 10
  • 45
  • 73
jsyemty
  • 21
  • 5
  • 1
    Is this python 3? If so then by default all inputs will be `str` you need to cast to `int` – EdChum May 28 '15 at 15:21
  • Because `int` < `str` is never going to be false. `while rounds < x`; – ZdaR May 28 '15 at 15:21
  • 1
    @EdChum That's also true of `raw_input` in Python 2 so it should definitely be fixed. – SuperBiasedMan May 28 '15 at 15:23
  • @SuperBiasedMan didn't know what about python 2, for some reason thought this was different between python 2 and 3 – EdChum May 28 '15 at 15:29
  • @EdChum, `input` in `Python3` = `raw_input` in `Python2`. `input` in `Python2` has no correlate in `Python3` and tries to assume the type of the input automatically. – NDevox May 28 '15 at 15:31
  • @EdChum @Scironic That's the gist of it. `ìnput` would technically work here but it's best to explicitly set it. – SuperBiasedMan May 28 '15 at 15:33
  • @Scironic thanks for the info, I don't use `input` and `raw_input` at all so it's useful to know this difference – EdChum May 28 '15 at 15:44

4 Answers4

10

raw_input returns a string, you need to cast it to int

x = int(raw_input("How many rounds:"))

Note that in python:

>>> 159 < "8"
True
>>> 159 < 8
False
>>> 

To better understand the int-string comparison, you can look here.

Community
  • 1
  • 1
Elisha
  • 4,811
  • 4
  • 30
  • 46
4

Change your input line to:

x = int(raw_input("How many rounds:"))

and you should be good to go. As pointed out in the comments, the default raw_input is a string, and comparing an integer to a string won't give you what you want.

Rob Fagen
  • 774
  • 1
  • 8
  • 24
3

Since raw_input() always returns a str So for homogeneous comparison, You need to change the type of x to int

import random

x = int(raw_input("How many rounds:"))

rounds = 0
ZdaR
  • 22,343
  • 7
  • 66
  • 87
0

And to be sure the user enters the needed value (i.g. "qwerty" or 2.6) you can add exception handling:

import random
try:
    x = input("How many rounds: ")
    rounds = 0
    while rounds < int(x):
        # rock=0, paper=1, scissors=2
        computer1 = random.randint(0,2)
        computer2 = random.randint(0,2)

        if computer1 == computer2:
            print ("draw")
        elif computer1 == 0 and computer2 == 1:
            print ("lose")
        elif computer1 == 1 and computer2 == 2:
            print ("lose")
        elif computer1 == 2 and computer2 == 0:
            print ("lose")
        else:
            print ("win")
        rounds = rounds + 1
except (ValueError):
    print('Wrong input')

This code is Python 3.

Alexey Gorozhanov
  • 706
  • 10
  • 20