我正在制作这个程序,要求你输入一个数字然后打印出该数字时间表的前1000个术语。我正在使用Python 3x输出应该是:
但它给了我这个:
这是代码:
multiplication = 0
firstnumber = int(input("Enter a number: "))
number = firstnumber
for j in range(0, 1001):
for i in range(0, 1001):
multiplication = multiplication+1
number = number*multiplication
print(str(multiplication) + " times " + str(firstnumber) + " is " + str(number))
由于
答案 0 :(得分:2)
在尝试开始编码之前,我发现更容易思考问题。
您迈出了第一步:从用户那里获取一个号码
我认为第二步包括从0到1000并乘以该数字。在伪代码中:
users_number = some_number
for num from 0 - 1000:
print(num * usernumber)
答案 1 :(得分:1)
您的问题是您更新了number
并继续相乘。您预见到了这个问题并创建了一个名为firstnumber
的变量来解决它,但您忘了使用它。这就是你的意思:
>>> multiplication = 0
>>> firstnumber = int(input("Enter a number: "))
Enter a number: 17
>>> number = firstnumber
>>> number = firstnumber
>>> for j in range(0, 1001):
... for i in range(0, 1001):
... multiplication = multiplication+1
... number = firstnumber * multiplication
... print(str(multiplication) + " times " + str(firstnumber) + " is " + str(number))
...
1 times 17 is 17
2 times 17 is 34
3 times 17 is 51
4 times 17 is 68
5 times 17 is 85
6 times 17 is 102
7 times 17 is 119
8 times 17 is 136
9 times 17 is 153
10 times 17 is 170
11 times 17 is 187
12 times 17 is 204
13 times 17 is 221
14 times 17 is 238
15 times 17 is 255
16 times 17 is 272
但是,做这样的事情可能会好得多:
number = int(input("Enter a number: "))
mult = int(input("How many multiples: "))
for i in range(mult+1):
print("%d times %d is %d" %(number, i, number*i))
答案 2 :(得分:0)
可能不是最好的代码,但比你尝试的要好。
given_number = int(input("Enter a number: "))
for multiplier in range(1,1001):
print("{0:4} times {1} is {2}".format(multiplier, given_number, multiplier*given_number))