python为变量分配多个字符串?如何运行此代码?

时间:2012-11-09 14:21:10

标签: python while-loop

所以即时新手在这里,我试图为剧院票务系统编写一个程序。 这只是我试图找出如何使用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 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:0)

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

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

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

答案 2 :(得分:0)

你想要这样的东西吗?:

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

>>> choose in movie_names
True

答案 3 :(得分:0)

dictionary比您当前使用的方法更有用。我不会修改脚本以根据需要进行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)]

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