我正在尝试创建一个基于文本的小型游戏。验证用户输入时遇到问题。如果可能的话,我希望有任何关于清理它的建议,或者修复不支持的操作数类型错误。
提前致谢。
def getPlayerName():
playerName = input("What is your name?")
print("\nHello, " + playerName + ".")
def getPlayerAge():
playerAge = input("What is your age?")
def getPlayerJob():
print("The current available occupations are as follows:\nA: Farmer,\nB: Soldier,\nC: Scientist")
playerJob = input("So which do you choose?")
if (playerJob == "A" | "a" | "Farmer" | "farmer"):
playerHp = 25
playerDamage = 15
playerPerception = 20
elif (playerJob == "B" | "b" | "Soldier" | "soldier"):
playerHp = 30
playerDamage = 20
playerPerception = 10
elif (playerJob == "C" | "c" | "Scientist" | "scientist"):
playerHp = 20
playerDamage = 10
playerPerception = 30
class player:
def __init__(self, name, age, job, hp, damage, perception):
self.name = name
self.age = age
self.job = job
self.hp = hp
self.damage = damage
self.perception = perception
答案 0 :(得分:0)
这是python中的无效语法:
playerJob == "A" | "a" | "Farmer" | "farmer"):
你想要它
if playerJob == 'a' or playerJob =='A' or playerJob == 'Farmer' ect
#OR MORE READABLE
if playerJob in ('a', 'A, 'Farmer', 'farmer'):
我已经清理了你的代码。我会建议你继续学习python中的基本语法。希望这可以帮助。
#!/usr/bin/env python
#-*- coding: utf-8 -*-
class player:
def __init__(self, name, age, job, hp, damage, perception):
self.name = name
self.age = age
self.job = job
self.hp = hp
self.damage = damage
self.perception = perception
# you need a function to send objects that controls the main
# flow of your program - main()
def main():
name = getPlayerName()
age = getPlayerAge()
job, hp, damage, perception = getPlayerJob()
#use the gathered variables to make your class
player_object = Player(
name, age,
job, hp,
damage, perception
)
def getPlayerName():
playerName = input("What is your name?")
print("\nHello, " + playerName + ".")
return playerName # return back to main
def getPlayerAge():
playerAge = input("What is your age?")
return playerAge # return back to main
def getPlayerJob():
print("The current available occupations are as follows:\n \
A: Farmer,\nB: Soldier,\nC: Scientist")
player.job = input("So which do you choose?")
if playerJob in ( "A", "a", "Farmer", "farmer"):
playerHp = 25
playerDamage = 15
playerPerception = 20
elif player.Job in ("B", "b", "Soldier", "soldier"):
playerHp = 30
playerDamage = 20
playerPerception = 10
elif (player.Job in ("C", "c", "Scientist", "scientist"):
playerHp = 20
playerDamage = 10
playerPerception = 30
else:
print("invalid selection try again:")
getPlayerJob()
return (job, playerHp,
playerDamage, playerPerception) # return all 4 values back to main in a single tuple
main()