如何定义用户输入?

时间:2016-12-11 11:10:47

标签: python

group

一旦玩家要输入像火星人一样的字符,就会发生以下错误:  文件“C:/Users/megam/Desktop/Martians/Martians.py”,第2行,in     character = input(str(“martian,plutonian,human or dog \ n>>”))#这会询问玩家他们想扮演什么角色   文件“”,第1行,in NameError:名称'martian'未定义

2 个答案:

答案 0 :(得分:1)

首先,你应该修复意图。 其次if character=="martian":您必须将其从:更改为==,以获得火星,狗,钚和人类。

if character=="martian":  # This makes the following only occur if the player inputs "martian"
    strength += 3  #This adds 3 to the value of strength
    speed +=2 #This adds 2 to the value of speed
if character=="plutonian":  #This makes the following only occur if the player inputs "plutonian"
    heart_rate+=7 #This adds 7 to the value of heart rate
    height1+=1 #This adds 1 to the value of height
if character=="human": #This makes the following only occur if the player inputs "human"
    heart_rate+=3 #This adds 3 to the heart rate value
if character=="dog":
    height-=3 #This adds 3

你也应该改变 character = input(str("martian,plutonian,human or dog\n>>"))

character = raw_input(str("martian,plutonian,human or dog\n>>"))

答案 1 :(得分:0)

更改为:

if character=="martian":  # This makes the following only occur if the player inputs "martian"
    strength += 3  #This adds 3 to the value of strength
    speed +=2 #This adds 2 to the value of speed
if character =="plutonian":  #This makes the following only occur if the player inputs "plutonian"
    heart_rate+=7 #This adds 7 to the value of heart rate
    height1+=1 #This adds 1 to the value of height
if character=="human": #This makes the following only occur if the player inputs "human"
    heart_rate+=3 #This adds 3 to the heart rate value
if character=="dog":
    height-=3 #This adds 3