我正在尝试制作一个项目,你可以选择击败龙。这就是我所拥有的:
import time
print "Hello"
time.sleep(1.5)
print "Welcome to Kill the Dragon."
time.sleep(2)
print "In this game, you will choose an option, and if you make the right choices, you will slay the dragon."
time.sleep(5)
print "You are walking through a forest on a cold, damp, windy night."
time.sleep(3)
print "You see a castle, and as you are looking for a shelter, you decide to try your luck there."
time.sleep(5)
print "You are greeted by a knight in shining armor. He gives you a questioning look. What do you say?"
time.sleep(4)
first = raw_input("1: Say you are looking for shelter from the storm " "2: Say you are lost and need food ")
time.sleep(5)
if first == 1:
print "Ok, we could all use some help from this bad storm outside. Please let me lead you to a good place."
time.sleep(5)
print "After a good dinner, you ask if there is anything you can do to help."
time.sleep(2)
if first == 2:
print "Ugg, I hate filthy peasants! If you go kill the dragon for us, maybe we will let you stay."
time.sleep(4)
print "1: Insist on getting inside the castle"
print "2: Ask the knight for armor, a weapon, and a steed"
second2 = raw_input()
问题是当我回答" 1"或" 2,"代码停止运行,它不会转到第一个== 1或第一个== 2
请告诉我原因,我是Python的新手。
答案 0 :(得分:1)
您的问题是raw_input()
将输入作为字符串。尝试向其投射int()
。
>>> x = raw_input("Enter: ")
Enter: 1
>>> x
"1"
>>> x = int(raw_input("Enter: "))
Enter: 1
>>> x
1
因此,这是您编辑的代码:
import time
print "Hello"
time.sleep(1.5)
print "Welcome to Kill the Dragon."
time.sleep(2)
print "In this game, you will choose an option, and if you make the right choices, you will slay the dragon."
time.sleep(5)
print "You are walking through a forest on a cold, damp, windy night."
time.sleep(3)
print "You see a castle, and as you are looking for a shelter, you decide to try your luck there."
time.sleep(5)
print "You are greeted by a knight in shining armor. He gives you a questioning look. What do you say?"
time.sleep(4)
first = int(raw_input("1: Say you are looking for shelter from the storm " "2: Say you are lost and need food "))
time.sleep(5)
if first == 1:
print "Ok, we could all use some help from this bad storm outside. Please let me lead you to a good place."
time.sleep(5)
print "After a good dinner, you ask if there is anything you can do to help."
time.sleep(2)
if first == 2:
print "Ugg, I hate filthy peasants! If you go kill the dragon for us, maybe we will let you stay."
time.sleep(4)
print "1: Insist on getting inside the castle"
print "2: Ask the knight for armor, a weapon, and a steed"
second2 = raw_input()
答案 1 :(得分:1)
raw_input
返回字符串,而不是整数:
>>> first = raw_input("1: Say you are looking for shelter from the storm " "2: Say you are lost and need food ")
1: Say you are looking for shelter from the storm 2: Say you are lost and need food 2
>>> first
'2'
>>> first == 2
False
>>> first == '2'
True
因此,请替换:
if first == 1:
使用:
if first == '1':
或与:
if int(first) == 1: