【发布时间】:2013-03-15 10:05:37
【问题描述】:
我仍在学习 Python,因为我想向 11 岁的孩子教授该语言的基本概念(我是一名教师)。我们在基础方面做了一些工作,因此他们了解编程和将任务分解成块等的要点。随着新课程的推出,Python 是一种将在英国各地教授的语言,我不想教孩子们坏习惯。下面是我编写的一个小程序,是的,我知道它很糟糕,但任何关于改进的建议都将非常感激。
我仍在学习该语言的教程,所以请保持温柔! :o)
# This sets the condition of x for later use
x=0
# This is the main part of the program
def numtest():
print ("I am the multiplication machine")
print ("I can do amazing things!")
c = input ("Give me a number, a really big number!")
c=float(c)
print ("The number", int(c), "multiplied by itself equals",(int(c*c)))
print("I am Python 3. I am really cool at maths!")
if (c*c)>10000:
print ("Wow, you really did give me a big number!")
else:
print ("The number you gave me was a bit small though. I like bigger stuff than that!")
# This is the part of the program that causes it to run over and over again.
while x==0:
numtest()
again=input("Run again? y/n >>>")
if x=="y":
print("")
numtest()
else:
print("Goodbye")
【问题讨论】:
-
你的问题到底是什么?如果您正在寻找代码审查,codereview.stackexchange 可能是一个更好的场所。
-
你有什么问题?
-
在
print("Goodbye")之后,仍然在else内,我会放置x = 1,这样当用户尝试退出时循环不会重复。还有,其他人回答的,需要查看if again=="y" -
避免在函数调用及其后面的括号之间放置空格。您可能(不确定)对 Python 2 有一些歧义,因为
print ("string", 10)将打印一个元组,但print("string", 10)将打印两个值。
标签: python loops while-loop