我怎么能改进我的代码

时间:2017-10-23 20:43:35

标签: python

我可以提供一些有关如何改进代码的提示吗?

这是我想改进的代码

timetable =(input('Please enter a number: ')
  if timetable ==('11')
    print(('Please enter a number less than 10 / equal to 10")) 
      for t in range(0, 16):
        print(str(t)+" timetable "=str(t)+" is "+str(t*t)

4 个答案:

答案 0 :(得分:1)

I'm not going to tell you exactly what to fix, because that is not how to learn. But here are some issues:

  1. If statements need a colon at the end
  2. Printing multiple things in one statement require a comma or plus sign between each of the things
  3. A string can begin and end with ' or ", but they need to be the same at the beginning or end.
  4. Won't give an error, but easy on the brackets, you have way more brackets than you need.
  5. Don't tab out every line

Generally, read the error message, see what line it applies to, google the name of the error, and, you know, work it out.

答案 1 :(得分:0)

代码中的语法有很多错误。我建议回顾一些介绍性的Python材料,以确保你理解括号,冒号等的含义。

请注意

  1. 您不需要在单个字符串周围放置括号。这只会让事情变得混乱。
  2. 尝试在您的运算符之间留出空格(+/==等。)这会让您的代码更容易阅读,更容易看出您是否让操作员在正确的位置。
  3. 括号必须始终匹配:如果打开它们,则必须关闭它们。

答案 2 :(得分:0)

显然,您必须了解缩进和其他答案中给出的其他内容。但是,如果你仍然希望重写你的代码,你可以看到下面的内容,如果你有python解释器,当你运行该文件时,你会在相应的行上看到错误。

timetable = int(input('Please enter a number: '))
if timetable > 10:
    print("Please enter a number less than 10 / equal to 10") 
for t in range(0, 16):
    print(str(t)+" timetable ="+str(t)+" is "+str(t*t))

答案 3 :(得分:0)

您在大多数变量周围添加了不必要的括号

我还建议您使用数字的整数输入

if timetable > 10:
    print("Please enter a number less than or equal to 10")

从您的代码中,您似乎想要输入<或=到10

for t in range(0, 16):
    print("{} timetable {} is {}".format(t, t, (t*t)))

将你的for循环移到if语句之外

还可以使用.format函数将文本格式化为字符串

timetable = int(input("Please enter a number: "))

if timetable > 10:
    print("Please enter a number less than or equal to 10")

for t in range(0, 16):
    print("{} timetable = {} is {}".format(t, t, (t*t)))

总之,你最终会得到像

这样的东西
{{1}}