簡體   English   中英

不支持的操作數類型? python3

[英]Unsupported Operand Types? python3

我正在嘗試創建一個小型的基於文本的游戲。 驗證用戶輸入時遇到麻煩。 如果可能的話,我很樂意提供清理或修復不受支持的操作數類型錯誤的任何建議。

提前致謝。

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

這是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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM