帮助"def"在python3中 [英] Help with "def" in python3

查看:83
本文介绍了帮助"def"在python3中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

a = input("say ''hi''")
def toyou()
	return a + "to you too"
print(toyou)
print(toyou + !)



我收到语法无效"错误.

我要发生的事情是接受输入并添加给您".然后我第一次打印,然后第二次打印!".到最后.

谢谢



I get an "invalid syntax" error.

What i want to happen is it to take the input and add "to you". Then i print that the first time, then the second an a "!" to the end.

Thank you

推荐答案

乍一看,我立即看到:缺少``:''.下一个错误:函数调用中缺少圆括号.另一个:在!"中没有引号.

必须为:
From the first glance I immediately see: lack of '':''. Next bug: lack of round brackets at the function call. Another one: lack of quotation marks in "!".

Must be:
a = input("say ''hi'' ")
def toyou()
    return a + " to you too"
print toyou()
print toyou() + "!"



一个普遍的问题:字符串连接要求将变量a分配给字符串.如果用户输入12,则toyou()将失败.它要求用户输入带引号的字符串文字. "12""hi"一样好.另外,请注意:我添加了两个额外的空格字符以改善输入提示和串联输出;您可以将输入"hi"与"toto you"连接起来,从而产生"hito you too".

另一个问题是在函数外部的上下文中使用变量a.您最好使用一个参数:



One general problem: string concatenation requires the variable a to be assigned to string. If the user input 12, toyou() will fail. It requires the user to input string literal with quotation marks; "12" would work as well as "hi". Also, pay attention: I added couple of extra blank space character to improve input prompt and output of concatenation; you would concatenate input "hi" with "to you too" resulting in "hito you too".

Another problem is using a variable a from the context outside of the function. You should better use a parameter:

def toyou(someValue)
    return someValue + " to you too"

a = input("say ''hi'' ")
print toyou(a)
print toyou(a) + "!"



我在发布之前测试了代码片段.

看,我在第二次帮助您解决几乎相同的问题(顺便说一句,感谢您接受我以前的解决方案).也许您需要改进您的工作/学习方法:更加注意先前的答案,首先阅读文档,然后做得更透彻,对每个小细节都更加关注.您的问题很容易解决.加快速度.

希望对您有帮助.

—SA



I tested the code fragments before posting.

Look, I''m helping you for the 2nd time with nearly the same question (thank you for accepting my previous solution, by the way). Maybe you need to improve your method of work/learning: pay more attention for the previous answers, read documentation first and do it more thoroughly, pay more attention of every small detail. Your problems are very easy; get to the speed.

Hope it will help you.

—SA


这篇关于帮助"def"在python3中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆