[英]Creating a 'user input' based multiplication table in python
I am attempting to create a program asking the user for two inputs, from this a multiplication table will be created.我正在尝试创建一个程序,要求用户提供两个输入,由此将创建一个乘法表。 For example, the user inputs 2 and 5.
例如,用户输入 2 和 5。
Enter a starting integer of less than 1,000 and greater than 0: 2输入小于 1,000 且大于 0 的起始整数:2
Enter an ending integer greater than the first number and less than 1,000 : 5输入大于第一个数字且小于 1,000 的结尾整数:5
I get something that looks like this:我得到的东西看起来像这样:
4 6 8 10
6 9 12 15
8 12 16 20
10 15 20 25
However the math is wrong and I want 2-5 printed at the top and on the left side.但是数学是错误的,我想在顶部和左侧打印 2-5 个。
This is what I have so far:这是我到目前为止:
# In this program I will help you make a multiplication table
print('In this program, I will help you make a multiplication table.')
print('\n')
# Ask user for a starting integer
start_value = int(input('Enter a starting integer of less than 1,000 and greater than 0: '))
while start_value < 1 and start_value > 1000:
start_value = int(input('Enter a starting integer of less than 1,000 and greater than 0: '))
# Ask user for an ending integer
end_value = int(input('Enter an ending integer greater than the first number and less than 1,000 : '))
while end_value < 0 and end_value > 1000 and end_value > start_value:
print('Invalid number')
end_value = int(input('Enter an ending integer greater than the first number and less than 1,000 : '))
for num1 in range(start_value, end_value+1):
for num2 in range(start_value, end_value+1):
table = (num1*num2)
print(format(table, '6d'), end = '')
print('\n')
I feel like something is up with the for-loop, I hope this makes sense, I greatly appreciate all the help!我觉得 for 循环出了点问题,我希望这是有道理的,我非常感谢所有的帮助!
Thank you!!!谢谢!!!
In this code here, you get input, then start a loop.在这里的这段代码中,您获得输入,然后开始一个循环。 The
input
function is what gets user input, and you never get it again. input
函数是获取用户input
函数,你再也不会得到它了。 You also have a logic error with your and
s, they should be or
s instead.你的
and
s 也有逻辑错误,它们应该是or
s 。
# this is your code, exactly as above
end_value = int(input('Enter an ending integer greater than the first number and less than 1,000 : '))
while end_value < 0 and end_value > 1000 and end_value > start_value:
print('Invalid number')
Easiest fix:最简单的修复:
end_value = int(input('Enter an ending integer greater than the first number and less than 1,000 : '))
while end_value < 0 or end_value > 1000 or end_value > start_value:
print('Invalid number')
end_value = int(input('Enter an ending integer greater than the first number and less than 1,000 : '))
But that duplicates the first line.但这重复了第一行。 So, a couple of other options:
因此,还有一些其他选择:
while True:
end_value = int(input('Enter an ending integer greater than the first number and less than 1,000 : '))
if end_value < 0 or end_value > 1000 or end_value > start_value:
break
print('Invalid number')
Or, if you are on python 3.8+, you can use the :=
syntax here或者,如果您使用的是 python 3.8+,则可以在此处使用
:=
语法
while (end_value := int(input('Enter an ending integer greater than the first number and less than 1,000 : '))) < 0 or end_value > 1000 or end_value > start_value:
print('Invalid number')
After working through this for a while, I did come up the answer to my own question.在解决了一段时间之后,我确实想出了我自己问题的答案。 I want to post it here for anyone else who may be interested in the solution.
我想把它贴在这里给任何可能对解决方案感兴趣的人。
# Max value as a constant
MAX_VALUE = 1000
# In this program I will help you make a multiplication table
print('In this program, I will help you make a multiplication table.')
print('\n')
# Ask user for a starting integer and give parameters
start_value = int(input('Enter a starting integer of less than 1,000 and greater than 0: '))
while start_value < 1 and start_value > MAX_VALUE:
start_value = int(input('Enter a starting integer of less than 1,000 and greater than 0: '))
print('\n')
# Ask user for an ending integer and give parameters
end_value = int(input('Enter an ending integer greater than the first number and less than 1,000 : '))
while end_value < 0 and end_value > MAX_VALUE and end_value > start_value:
print('Invalid number')
end_value = int(input('Enter an ending integer greater than the first number and less than 1,000 : '))
print('\n')
# Format the top corner space
print(' ', end = '')
# Add a label row
for label in range(start_value, end_value +1):
print(format(label, '6d'), end = '')
print('\n')
# Multiplication chart and left label column
for num1 in range(start_value, end_value+1):
print(format(num1, '6d'), end = '')
for num2 in range(start_value, end_value+1):
table = (num1*num2)
print(format(table, '6d'), end = '')
print('\n')
This makes a clean Multiplication table for any range of numbers less than 1,000.这为小于 1,000 的任何数字范围创建了一个干净的乘法表。
Cheers!干杯!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.