Python 3.x 基本Python 3问题的语法错误
可能重复:Python 3.x 基本Python 3问题的语法错误,python-3.x,Python 3.x,可能重复: 我知道Python应该是解释语言之神。。。它的简单,它没有冗余,诸如此类。但是,我太习惯于C,C++,java,JavaScript和PHP,我必须承认这很烦人。这是我的密码: #!/usr/bin/python3.1 def shit(texture, weight): if textura is "green": texturaTxt = "Shit is green" elif textura is "blue": textur
我知道Python应该是解释语言之神。。。它的简单,它没有冗余,诸如此类。但是,我太习惯于C,C++,java,JavaScript和PHP,我必须承认这很烦人。这是我的密码:
#!/usr/bin/python3.1
def shit(texture, weight):
if textura is "green":
texturaTxt = "Shit is green"
elif textura is "blue":
texturaTxt = "Shit is blue"
elif textura is "purple":
texturaTxt = "Shit is purple"
else:
print "Incorrect color"
break
if weight < 20:
pesoTxt = " and weights so few"
elif weight >= 20 and peso <=50:
pesoTxt = " and weights mid"
elif weight > 50:
pesoTxt = " and weights a damn lot"
else:
print "Incorrect weight"
return texturaTxt + pesoTxt
c = input("Introduce crappy color: ")
e = input("Introduce stupid weight: ")
r = shit(c, w)
print r
#/usr/bin/python3.1
def粪便(质地、重量):
如果纹理为“绿色”:
texturaTxt=“狗屎是绿色的”
elif textura为“蓝色”:
texturaTxt=“狗屎是蓝色的”
elif textura为“紫色”:
texturaTxt=“大便是紫色的”
其他:
打印“错误颜色”
打破
如果重量<20:
pesoTxt=“而且重量很少”
elif重量>=20和比索50:
pesoTxt=“而且重量很大”
其他:
打印“重量不正确”
返回纹理ATXT+pesoTxt
c=输入(“引入蹩脚颜色:”)
e=输入(“引入愚蠢的权重:”)
r=屎(c,w)
印刷机
我正在尝试学习Python,我试图实现的目标是:
...
function shit(texture, weight) {
string textureTxt = "Shit is ", pesoTxt = " and weights ";
switch(texture) {
case "green": textureTxt .= "green as an applee"; break;
case "blue": textureTxt .= "blue as the sky"; break;
case "purple": textureTxt .= "purple as Barny"; break;
default: cout<<"Incorrect texture, try again later" <<endl; exit;
}
//etc
}
string color = "";
int weight = 0;
cout<<"Introduce color: ";
cin>>color;
//etc
cout<<shit(color, weight);
...
。。。
功能(纹理、重量){
字符串textureTxt=“Shit is”,pesoText=“and weights”;
切换(纹理){
案例“绿色”:TextureText.=“作为被申请者的绿色”;中断;
case“blue”:textureTxt.=“像天空一样蓝”;break;
案例“紫色”:TextureText.=“紫色如谷仓”;中断;
默认值:coutPython3不再支持将print作为一种特殊形式,并在print关键字后面加上参数(如python2.x中的print x
)。
相反,print
现在是一个函数,需要一个参数列表,如:
打印(x)
。请参阅中的“打印是一项功能”
此外,break
语句不能出现在循环之外。除了Cswitch
语句之外,if
不支持break。由于if语句没有贯穿逻辑,因此不需要中断。要停止函数执行并返回调用方,请使用return
使用相等运算符==
代替is
测试对象是否相同,这是比相等更严格的测试。
可以找到更多细节
此外,您将以字符串形式获取权重。您可能希望使用函数int(weight)
将字符串转换为整数,以便与其他整数值进行比较
此外,还有其他一些小错误:
- 当您尝试在函数调用中使用未定义的变量名
w
时,您正在将权重的用户输入分配给e
- 函数中的第一个参数称为
texture
,但您正在函数体中使用textura
- 您在一个实例中使用的是
peso
而不是weight
下面是一个(不那么冒犯性的)重写,消除了这些错误:
def stuff(color, weight):
color_txt = "Your stuff is "
if color == "green":
color_txt += "green as an apple"
elif color == "blue":
color_txt += "blue as the sky"
elif color == "purple":
color_txt += "purple as an eggplant"
else:
print("Invalid color!")
return
w = 0
# converting a string to an integer might
# throw an exception if the input is invalid
try:
w = int(weight)
except ValueError:
print("Invalid weight!")
return
weight_txt = ""
if w<20:
weight_txt = " and weighs so few"
elif 20 <= w <= 50:
weight_txt = " and weighs mid"
elif w > 50:
weight_txt = " and weighs a lot"
else:
print("Invalid weight!")
return color_txt + weight_txt
c = input("Input color: ")
w = input("Input weight: ")
r = stuff(c, w)
print(r)
def物料(颜色、重量):
color\u txt=“你的东西是”
如果颜色=“绿色”:
color_txt+=“像苹果一样绿”
elif颜色==“蓝色”:
color_txt+=“像天空一样蓝”
elif颜色==“紫色”:
color_txt+=“紫色如茄子”
其他:
打印(“无效颜色!”)
返回
w=0
#将字符串转换为整数可能会
#如果输入无效,则引发异常
尝试:
w=int(重量)
除值错误外:
打印(“无效重量!”)
返回
权重_txt=“”
如果w