python - 学习 Python 并需要帮助在不破坏其余代码的情况下移动符号
问题描述
我正在学习 Python,需要一些帮助来将我的符号移到左边的几个空格上。我已经完成了作业,我不寻求家庭作业帮助。我只是希望它在我的末端看起来更好。在我在下面链接中添加的图片中,您可以看到“#”太靠右了。我希望它坐在偶数旁边。我尝试在我的打印语句中留出更多空间,但所做的只是移动我的整个表格。如何让“#”紧挨着我的偶数?
这是我的代码。
#ask for user input on size of Multiplication Table.
size = int(input("What size Multiplication Table would you like? (2-10):"))
while (size <2) or (size >10):
print("Invalid entery! Enter a Number Between 2 and 10")
size = int(input("What size Multiplication Table would you like? (2-10):"))
print()
print()
#dispaly header
print(" --- Multiplication Table(",size,"x",size,") ---")
print(" ",end="")
size += 1
for h in range(1,11):
if h == 10:
print(" ",h, end=" ")
else:
print(" ",h, end=" ")
print()
for h in range(1,100):
print('-',end='')
print()
#display Multiplication Table
#outer loop
for a in range(1,size):
if a ==10:
print('',a,'|',end='')
else:
print('',a,' |',end='')
#inner loop
for b in range(1,size):
result = a * b
if result >=100:
print(' ',result, end=' ')
elif result >=10:
print(' ',result, end=' ')
else:
print(' ', result, end=' ')
# for putting '#' at the end of even numbers
if result %2==0:
print('#', end='')
elif result == 100:
print('', end='')
else:
print(' ', end='')
print()
解决方案
我对您的内部循环进行了最小的修改,以产生所需的效果:
for b in range(1,size):
result = a * b
# for putting '#' at the end of even numbers
if result %2==0:
end_str='#'
elif result == 100:
end_str=''
else:
end_str=' '
if result >=100:
print(' ',result, end=end_str+' ')
elif result >=10:
print(' ',result, end=end_str+' ')
else:
print(' ', result, end=end_str+' ')
输出,经过此修改:
1 2 3 4 5 6 7 8 9 10
---------------------------------------------------------------------------------------------------
1 | 1 2# 3 4# 5 6# 7 8# 9 10#
2 | 2# 4# 6# 8# 10# 12# 14# 16# 18# 20#
3 | 3 6# 9 12# 15 18# 21 24# 27 30#
4 | 4# 8# 12# 16# 20# 24# 28# 32# 36# 40#
5 | 5 10# 15 20# 25 30# 35 40# 45 50#
6 | 6# 12# 18# 24# 30# 36# 42# 48# 54# 60#
7 | 7 14# 21 28# 35 42# 49 56# 63 70#
8 | 8# 16# 24# 32# 40# 48# 56# 64# 72# 80#
9 | 9 18# 27 36# 45 54# 63 72# 81 90#
10 | 10# 20# 30# 40# 50# 60# 70# 80# 90# 100#
推荐阅读
- php - 如何将可折叠面板的状态保存到会话变量?
- java - 是否可以将 Java 的 Socket API 与 Spring-Boot Web 应用程序一起使用?
- r - 计算同一data.frame中两个长/纬点之间的距离
- python - 如何保存在交互模式下创建的变量?
- python-3.x - 进度条滞后于单独线程 tkinter 上的计算密集型函数
- android - NativeScript ListView 没有更新
- visual-studio - 为什么我可以在 VS2019 中安装 NuGet v4.0.10 而在 VS2012 中却不能?
- ios - 尝试在时间间隔后清除粘贴板
- laravel - 每个返回的 vue 组件都无法读取未定义的属性“parseComponent”
- python - 为套接字连接配置 Nginx