" HFormBio"将执行BioFam2和BioFam3,但跳过BioFam1的问题。 我究竟做错了什么?或者,我怎样才能做得更好?
全局变量正在调用x =''用于输入的容器。
我想询问BioFam1,如果FamHero ='是' ,但这个问题被忽略了,但BioFam2和BioFam3却没有。
def HFormBio ():
global FamHero
global FamDead
global BioFam1
global BioFam2
global BioFam3
if FamHero == 'Yes':
BioFam1 = input('What is their Name(s)? ')
return BioFam1
if FamDead == 'Yes':
BioFam2 = input('Were they supers as well? ')
BioFam3 = input('Were they registered Heroes? ')
return BioFam2, BioFam3
def HForm ():
global FirstName
global LastName
global HeroName
global Predest
global Nature
global Gender
global Attraction
global FamDead
global HeroApp
print(HeroApp)
FirstName = input('What is your First Name? ')
LastName = input('What is your Last Name? ')
HeroName = input('What is your Hero Name? ')
Nature = input('Are you Good or Evil? ')
Gender = input('Are you Male or Female? ')
Attraction = input('Are you attracted to Men or Women? ')
FamHero = input('Do you have any family that are registered Heroes? ')
FamDead = input('Are any of your family members dead due to murder? ')
return FirstName, LastName, HeroName, Predest, Nature, Gender, Attraction, FamHero
HForm ()
HFormBio ()
License = ' DEPARTMENT FOR THE REGULATION OF POWERED RESOURCES:\n\n HERO LICENSE: #13337\n Name: {0}\n Gender: {1}\n Age: 20\n Marital Status: Single \n Attracted to: {2}\n Hero Name: {3}\n Nature: {4}\n Attracted to: {5}\n Registered Family: {6}\n'.format(FirstName, Gender, Attraction, HeroName, Nature, Attraction, FamDead)
return License
输出:
Hero Application: Form 0113.4
What is your First Name? Daniel
What is your Last Name? Lewis
What is your Hero Name? W
Are you Good or Evil? Good
Are you Male or Female? Male
Are you attracted to Men or Women? Women
Do you have any family that are registered Heroes? Yes
#BioFam1's question is missing.
Are any of your family members dead due to murder? Yes
#BioFam2/3 are executing
Were they supers as well? Yes
Were they registered Heroes? Yes
DEPARTMENT FOR THE REGULATION OF POWERED RESOURCES:
HERO LICENSE: #13337
Name: Daniel
Gender: Male
Age: 20
Marital Status: Single
Attracted to: Women
Hero Name: W
Nature: Good
Attracted to: Women
Registered Family: Yes
答案 0 :(得分:0)
FamHero未在HForm中宣布为全球性。
你真的应该创建一个Hero类并使用类变量,如self.FamHero
答案 1 :(得分:0)
在Eric指出我没有声明适当的全局之后,我发现了如何做我想要的。 我改变了下面的代码。
print(HeroApp)
FirstName = input('What is your First Name? ')
LastName = input('What is your Last Name? ')
HeroName = input('What is your Hero Name? ')
Nature = input('Are you Good or Evil? ')
Gender = input('Are you Male or Female? ')
Attraction = input('Are you attracted to Men or Women? ')
FamHero = input('Do you have any family that are registered Heroes? ')
if FamHero == 'Yes':
BioFam1 = input('What is their Name(s)? ')
FamDead = input('Are any of your family members dead due to murder? ')
if FamDead == 'Yes':
BioFam2 = input('Were they supers as well? ')
BioFam3 = input('Were they registered Heroes? ')
return FirstName, LastName, HeroName, Predest, Nature, Gender, Attraction, FamHero, BioFam1, BioFam2, BioFam3