gpt4 book ai didi

python - 如何定义用户输入?

转载 作者:太空宇宙 更新时间:2023-11-03 15:53:02 25 4
gpt4 key购买 nike

from random import randint  # This imports the required modules for the program to work
character = input(str("martian,plutonian,human or dog\n>>")) # This asks the player what character they would like to play
strength = (randint(3, 20)) # This sets the range for the strength variable
speed = (randint(3, 20)) # This sets the range for the speed variable
height = randint(120, 183) # This sets the range for the height variable (the variables are 100x the amount that the task states)
# The reason as to why the height value is 100x what it is meant to be is because the height is a decimal and the randint function only works with real numbers
height1 = height/100 # Because the values are 100x the required amount I divide them by 100
heart_rate = (randint(60, 110)) # This sets the range for the heart rate variable


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


print("Your strength is", strength) # This prints the strength of the character
print("Your speed is", speed) # This prints the speed of the character
print("Your heart rate is", heart_rate) # This prints the heart rate of the character
print("Your height is", height1) # This prints the height of the character


file_txt=open('Character Statistics.txt', 'a',) #This creates and names the text file
file_txt.write(str("Character Statistics:\n")) #This makes a title within the text document
file_txt.write("Your strength is:") #This prints text into the text file
file_txt.write(str(strength)+"\n") #This adds the strength value to the txt file
file_txt.write("Your speed is:") #This prints text into the text file
file_txt.write(str(speed)+"\n") #This adds the speed value to the txt file
file_txt.write("Your heart rate is:") #This prints text into the text file
file_txt.write(str(heart_rate)+"\n") #This adds the heart rate value to the txt file
file_txt.write("Your height is is:") #This prints text into the text file
file_txt.write(str(height1)+"\n") #This adds the height value to the txt file

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

最佳答案

首先你应该确定意图。其次,对于火星人、狗、冥王星人和人类,if character==“martian”:必须将其从:更改为==。 p>

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("火星人、冥王星人、人类或狗\n>>"))

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

关于python - 如何定义用户输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41085488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com