Billjk
Billjk

Reputation: 10686

How do if statements work?

I am making a text based russian roulette game in Python, but I am not nearly finished, as one can see just by looking at my code:

#!/usr/bin/env python
print("The maximum number of bullets is 3")
bulletcounter = input("How many bullets do you want your gun to have?")
print(bulletcounter, "bullets")
paname = input("Enter Player 1's Name: ")
pbname = input("Enter Player 2's Name: ")
print(paname.capitalize(), "Vs.", pbname.capitalize())
if bulletcounter == 1:
    bulletcount = 0
    print(bulletcount)
    bulletaloc = random.randint(1, 6)
    while bulletaloc != bulletcount:
        bulletcount += 1

For some reason, even if someone enters 1 into bulletcounter, it doesn't trigger the if statement: if bulletcounter == 1. How do I make it trigger the if statement?

Upvotes: 4

Views: 1179

Answers (1)

imm
imm

Reputation: 5919

Use raw_input for your paname and pbname variables. Be sure to import random at the top of your file. It would also be better to use int(raw_input("How many...")) for bulletcounter, too, I think, than input, since this can be used to evaluate any arbitrary python code.

Also, it would be worth checking to see which version of Python you are using, when you invoke it using the env command. If, at the command line, you run:

/usr/bin/env python -V

and are getting "Python 2.x.y" instead of Python 3, and you are expecting to be using Python 3, consider changing that first line to call your Python 3 interpreter instead. The recommendations noted above assume you are using Python 2.

Upvotes: 4

Related Questions