python中的最后一个else子句不起作用
当我运行该程序并将性别输入为除男性或女性以外的任何其他内容时,该程序无法按预期工作。我挣扎了一天,弄不明白。还有,我怎样才能在调用函数之后仍然让程序运行呢?我读过一个main函数,但不知道如何用它修改代码python中的最后一个else子句不起作用,python,Python,当我运行该程序并将性别输入为除男性或女性以外的任何其他内容时,该程序无法按预期工作。我挣扎了一天,弄不明白。还有,我怎样才能在调用函数之后仍然让程序运行呢?我读过一个main函数,但不知道如何用它修改代码 from sys import exit birthdays = {'Alice': 'April 1', 'Bob': 'Dec 12', 'Carol': 'Mar 4'} def dateOfBirth(): bday = raw_input("> ") birt
from sys import exit
birthdays = {'Alice': 'April 1', 'Bob': 'Dec 12', 'Carol': 'Mar 4'}
def dateOfBirth():
bday = raw_input("> ")
birthdays[name] = bday
print "Database records updated."
print "The number of records is now " + str(len(birthdays)) + "."
print birthday`enter code here`s
exit(0)
while True:
print "Enter a name: (blank to quit)"
name = raw_input("> ")
if name == '':
exit(0)
if name in birthdays:
print birthdays[name] + " is the birthday of " + name + "."
exit(0)
else:
print "I do not have the birthday information for " + name + "."
print " What is the sex?"
gender = raw_input("> ")
if gender == 'male':
print "What is his birthday?"
dateOfBirth()
elif gender == 'female' or 'Female':
print "What is her birthday?"
dateOfBirth()
else:
print " The gender is not recognised. Anyway we will continue."
dateOfBirth()
更改此项:
elif gender == 'female' or 'Female':
为此:
elif gender in ('female', 'Female'):
或者我建议:
elif gender.lower()=='female':
你和我有什么关系
elif gender == 'female' or 'Female':
如果性别是女性,或者字符串“女性”是非空的,则本质上是else。无论
gender
的值是否按预期工作,这是怎么回事?似乎您必须坚持在这里==>而true:name=raw\u input(“>”)。您可以使用return
而不是exit(0)
@obayhan-是的,exit(0)
同时while下的所有代码都缩进,因此这不是一个问题problem@cricket_007不,我写的时候没有,如果你查一下历史的话。谢谢你,问题的第二部分如何。例如,请参阅如何使用main
函数。