问题描述
我正在使用Python制作基于文本的俄罗斯轮盘游戏,但我还没有完成,因为只要查看我的代码就可以看到:
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
出于某种原因,即使有人将 1
输入 bulletcounter
,也不会触发 if
语句:如果bulletcounter == 1
。如何触发 if
语句?
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?
推荐答案
使用 paname 和 pbname
变量的code> raw_input 。请务必在文件顶部导入随机
。对于 bulletcounter
,也可以更好地使用 int(raw_input(多少......))
我认为,比输入
,因为这可用于评估任何任意python代码。
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.
此外,它当您使用 env
命令调用它时,值得检查您正在使用的Python版本。如果在命令行中运行:
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
并且正在获得Python 2.xy而不是Python 3,并且您期望使用Python 3,请考虑先更改它换行调用Python 3解释器。上面提到的建议假设您使用的是Python 2。
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.
这篇关于if语句如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!