-1

任何人都可以帮我处理我的代码。我写了一个小程序,以便我可以了解一些功能以及发生了什么。让我告诉你我的代码。

print "Hello student need help multiplying by any mutiplicaton"
student = raw_input("If so then tell me which ones? => ").lower()
for i in range(0, 11):
    if student == str(0):
       i_num = 0 * i
       print "0 times %d equals %d" % (i,i_num)
    elif student == str(10):
       i_num = 1 * 1
       print "1 times %d equals %d" % (i,i_num)
else:
    print "Try this program when you can't figure it out your multiplications."

如果学生输入的内容与 if 语句无关,它将打印 else。但是,如果学生输入 str(1),它会打印乘法并打印 else,这是我不想发生的代码问题。谁能帮我。我只是想学习 if 和 elif 的这个功能等等。

4

3 回答 3

1

当前else设置为运行就成功完成了for循环,看看缩进。如果将它移到循环内,一切正常。您的代码确实包含一些语法错误。

您的程序的固定版本:

print "Hello student need help multiplying by any mutiplicaton"
student = raw_input("If so then tell me which ones? => ") # no need for lower()
for i in range(0, 11):
    if student == '0':
        i_num = 0 * i
        print "0 times %d equals %d" % (i,i_num)
    elif student == '1':
        i_num = 1 * 1
        print "1 times %d equals %d" % (i,i_num)
    else:
        print "Try this program when you can't figure it out your multiplications."

但是乘法很容易,所以为什么不这样做:

print "Hello student need help multiplying by any mutiplicaton"
num = int(raw_input("If so then tell me which ones? => "))
for i in range(0, 11):
    print "%d times %d equals %d" % (num, i, i * num)
于 2013-04-12T21:17:48.437 回答
0

你在这里缺少一个):

elif student == str(10):
于 2013-04-12T21:23:56.773 回答
0
def isAnInt(s):
   try: 
      int(s)
      return True
   except ValueError:
    print "You must enter a number"


print "Hello student need help multiplying by any mutiplicaton"
student = raw_input("If so then tell me which ones? => ")
if(isAnInt(student)):
  number_to_calc = int(student)
  for i in range(0,11):
    i_num = number_to_calc * i
    print "%d times %d equals %d" % (number_to_calc, i, i_num)
于 2013-04-12T21:36:38.020 回答