4

我正在尝试教我儿子如何编程,我给了他一个我自己无法完成的挑战。

他必须提示用户输入 A、B 或 C。根据他们的选择,他将打印一些结果和其他内容。当我们完成这项工作时,我正在尝试进行一些错误处理并检查输入不是 A、B 或 C 的情况。下面列出了代码。我当然感谢您提供的任何指导。

print "************************************************************"
print "*                                                          *"
print "*            Welcome to the cinemas                        *"
print "*                                                          *"
print "************************************************************"
print "*                                                          *"
print "*  What movie would you like to see ?                      *"
print "*                                                          *"
print "*  A) Star Wars                                            *"
print "*  B) Bourne Identity                                      *"
print "*  C) The Hunger Games                                     *"
print "*                                                          *"
print "************************************************************"
movie=raw_input()
while(movie!="A","B","C"):
    print "************************************************************"
    print "*                                                          *"
    print "*            Welcome to the cinemas                        *"
    print "*                                                          *"
    print "************************************************************"
    print "*                                                          *"
    print "*  What movie would you like to see ?                      *"
    print "*                                                          *"
    print "*  A) Star Wars                                            *"
    print "*  B) Bourne Identity                                      *"
    print "*  C) The Hunger Games                                     *"
    print "*                                                          *"
    print "************************************************************"
    movie=raw_input()

print "************************************************************"
print "*                                                          *"
print "*  How many tickets would you like ?                       *"
print "*                                                          *"
print "************************************************************"
quantity =input()
cost = 7.5
if movie =="A":
    print "You are seeing Star Wars"
    price = cost*quantity
    print "You owe ", price
elif movie =="B":
    print "You are seeing Bourne Identity"
    price = cost*quantity
    print "You owe ", price
elif movie =="C":
    print "You are seeing The Hunger Games"
    price = cost*quantity
    print "You owe ", price 
4

3 回答 3

7

你想做的while movie not in ("A", "B", "C")

movie != "A", "B", "C"检查是否movie等于三元素元组("A", "B", "C"),它永远不会。

于 2013-01-21T03:38:05.313 回答
1

这里有一些更好的方法来构建你的循环以避免重复。

第一种方法是将电影设置为无效值,这意味着循环将始终至少执行一次

movie = None
while movie not in ("A", "B", "C"):
    print "************************************************************"
    print "*                                                          *"
    print "*            Welcome to the cinemas                        *"
    print "*                                                          *"
    print "************************************************************"
    print "*                                                          *"
    print "*  What movie would you like to see ?                      *"
    print "*                                                          *"
    print "*  A) Star Wars                                            *"
    print "*  B) Bourne Identity                                      *"
    print "*  C) The Hunger Games                                     *"
    print "*                                                          *"
    print "************************************************************"
    movie = raw_input()

第二种方法是使用while True:循环

while True:
    print "************************************************************"
    print "*                                                          *"
    print "*            Welcome to the cinemas                        *"
    print "*                                                          *"
    print "************************************************************"
    print "*                                                          *"
    print "*  What movie would you like to see ?                      *"
    print "*                                                          *"
    print "*  A) Star Wars                                            *"
    print "*  B) Bourne Identity                                      *"
    print "*  C) The Hunger Games                                     *"
    print "*                                                          *"
    print "************************************************************"
    movie = raw_input()
    if movie in ("A", "B", "C"):
        break

然后您可以按照 Alex 的建议将电影存储在变量中

于 2013-01-21T04:14:43.800 回答
1

此外,您可以使用多行字符串,而不是打印每一行:

welcometext = """
************************************************************
*                                                          *
*            Welcome to the cinemas                        *
*                                                          *
************************************************************
*                                                          *
*  What movie would you like to see ?                      *
*                                                          *
*  A) Star Wars                                            *
*  B) Bourne Identity                                      *
*  C) The Hunger Games                                     *
*                                                          *
************************************************************"""

这样,您的程序可以非常紧凑:

print welcometext
movie = raw_input(">>")
while movie.upper() not in ("A","B","C"):
    print welcometext
    movie = raw_input(">>")

如果你想做一些更高级的事情:

movies = {
    "A": "Star Wars", 
    "B": "Bourne Identity", 
    "C": "The Hunger Games", 
    "D": "Kill Bill"
}

welcometext = """
************************************************************
*                                                          *
*            Welcome to the cinemas                        *
*                                                          *
************************************************************
*                                                          *
*  What movie would you like to see ?                      *
"""

for letter, movie in movies.items():
    welcometext += "*  {}) {: <52} *\n".format(letter, movie)

# ( <52 is for padding 52 spaces, \n is for the newline )

welcometext += """*                                                          *
************************************************************"""

movieletter = ''
while movieletter.upper() not in movies:
    print welcometext
    movieletter = raw_input(">>")

moviename = movies[movieletter.upper()]
print "You have selected {}!".format()

然后稍后您也可以将价格等添加到movies字典中。享受!

于 2013-01-21T03:58:56.053 回答