1

所以我是新手,我正在尝试为剧院票务系统编写程序。这只是我试图找出如何使用 while ,所以如果输入错误,我可以阻止程序执行下一步,这是 while 代码:(我在某个地方得到了它,我改变了一些东西)

Movie_is = "a"# i want to assign more than one string like "A","b","B" etc
movie_slection = True

while movie_slection:
Choose=raw_input("Choose your movie by typing the correct letter")
if Choose != Movie_is:
    print 'Wrong input'
    movie_slection = False 
elif Choose == Movie_is:
    print 'Your movie is :'

else:
print 'Wrong input.'


print 'Done'</i>

这是我想在其中使用第一个代码的另一个代码,如果我输入 d 它会进入门票选择:

print "a.Fight Club (1999)", "b.Freaks (1932)","c.Barton Fink (1992)"

Movie_is=raw_input("Choose your movie by typing the correct letter")
if Movie_is == "a" or Movie_is== "A":
 movie = "Fight Club (1999)"
elif Movie_is == "b" or Movie_is=="B":
 movie = "Freaks(1932)"
elif Movie_is == "c"  or Movie_is== "C":
 movie = "Barton Fink (1992)"
else:
 movie = "You have entered a wrong letter ,\n note:this application is case sensitive"
print " Selected Movie : ", movie

# the tickets selection
Tickets_is=raw_input(" Select Tickets: a.(Adult) b.(Children) c.(Senior) d.(Student)")
if Tickets_is=="a" or Tickets_is== "A"  :  
tickets="""
    ||       SCREEN         ||

     1 2 3 4 5 6 7 8 9 10


  """

elif Tickets_is == "b" or Tickets_is=="B":
 tickets= """
    ||       SCREEN         ||

     1 2 3 4 5 6 7 8 9 10


  """
elif Tickets_is == "c" or Tickets_is== "C":
 tickets= """
    ||       SCREEN         ||

     1 2 3 4 5 6 7 8 9 10


  """
elif Tickets_is == "d" or Tickets_is== "D":
 tickets= """
    ||       SCREEN         ||

     1 2 3 4 5 6 7 8 9 10


  """

else:
 tickets= "You have entered a wrong input , please type a valid seat number"
print "Seats: ",tickets </i>

提前致谢 。

4

4 回答 4

2

您将需要使用列表而不是字符串:

movies = [ 'a', 'b', 'c' ]
for movie in movies:
    print movie

但是你可能想要一个来定义一部电影。这使您可以在一个地方收集有关一部电影的所有信息。

于 2012-11-09T14:26:14.857 回答
0

您将需要查看 Python 列表。像这样的东西

movie_is = ['a', 'b', 'c']
if choice not in movie_is:
    print 'Wrong input'

有关列表的更多信息,请参阅http://docs.python.org/2/tutorial/datastructures.html

于 2012-11-09T14:27:42.920 回答
0

你想要这样的东西吗?:

>>> choose = "a"
>>> movie_names = ["A", "a"]

>>> choose in movie_names
True
于 2012-11-09T14:28:14.423 回答
0

字典将比您当前使用的方法有用得多。我不会根据您的需要修改脚本来执行 while 循环,但您应该能够轻松地将其转换。

movies={'a':'Movie A','b':Movie B'}
if choice.lower() is not in movies.keys():
     print 'Invalid choice. Try again'
else:
     print 'You have selected '+movies[tolower(choice)]

正如在其他地方所说,类将是一个更好的选择,因为您可以包含更多数据。但是如果你确定你只想要电影的名字,你可以这样做。

于 2012-11-12T23:04:22.527 回答