如何将用户输入的值变成一个可用值的变量?
How can I make the value of a user's input into a variable with a usable value?
from turtle import*
branch_width=4
grass_color=input("Would you like your grass to be snowy, dead, or healthy? ")
sky_color=input("Would you like it to be morning, afternoon, or evening? ")
morning="cadetblue"
afternoon="cornflowerblue"
evening="royalblue"
snowy="snow"
dead="wheat"
healthy="darkgreen"
每次我尝试将输入用作 color(sky_color)
时,用户的输入都不会转换为 morning="cadetblue"
,等等,这 returns 是一个错误。我该如何解决?我是否只需要让用户输入颜色?
您混淆了变量名和字符串。
您将名为 morning 的变量设置为字符串 "cadetblue"。
这与输入字符串 "morning" 无关。 您必须明确设置。
字典会有所帮助:
interpret_color = {
"morning": "cadetblue",
"afternoon": "cornflowerblue",
"evening": "royalblue",
"snowy": "snow",
"dead": "wheat",
"healthy": "darkgreen"
}
现在你可以试试
color(interpret_color(sky_color))
这让你感动吗?
from turtle import*
branch_width=4
grass_color=input("Would you like your grass to be snowy, dead, or healthy? ")
sky_color=input("Would you like it to be morning, afternoon, or evening? ")
morning="cadetblue"
afternoon="cornflowerblue"
evening="royalblue"
snowy="snow"
dead="wheat"
healthy="darkgreen"
每次我尝试将输入用作 color(sky_color)
时,用户的输入都不会转换为 morning="cadetblue"
,等等,这 returns 是一个错误。我该如何解决?我是否只需要让用户输入颜色?
您混淆了变量名和字符串。 您将名为 morning 的变量设置为字符串 "cadetblue"。 这与输入字符串 "morning" 无关。 您必须明确设置。
字典会有所帮助:
interpret_color = {
"morning": "cadetblue",
"afternoon": "cornflowerblue",
"evening": "royalblue",
"snowy": "snow",
"dead": "wheat",
"healthy": "darkgreen"
}
现在你可以试试
color(interpret_color(sky_color))
这让你感动吗?