1、斐波那契数列

斐波那契数列,数列前两项为1,之后每一项都是前两项之和。

#!/usr/bin/env python3
a, b = 0, 1
while b < 100:
print(b)
a, b = b, a + b

默认print输出结果后会自动换行,如果不希望换行,只做间隔的话,就通过另一个参数end来替换这个换行符

print(a, end=' ')

2、幂级数。

写一个程序计算幂级数:e^x = 1 + x + x^2 / 2! + x^3 / 3! + ... + x^n / n! (0 < x < 1)。

#!/usr/bin/python3
x = float(input("Enter the value of x:")) n = term = 1
result = 1.0
while n <= 100:
term *= x/n
result += term
n += 1
if term < 0.0001:
break
print("No of Times={} and Sum = {}".format(n, result))

3、乘法表

打印10以内的乘法表。

#!/usr/bin/env python3
i = 1
print('-' * 60)
while i < 11
n = 1
while n <= 10:
print("{:d}*{:d}={:d}".format(n, i, i * n), end=" ")
n += 1
print()
i += 1
print('-' * 60)
  • print('-' * 60):一个字符串重复60次输出

4、打印星号

打印各种形状的星号

  • 向上的直角三角
#!/usr/bin/env python3
n = int(input('Enter the number of rows:'))
i = 1
while i <= n:
print('*' * i)
i += 1
  • 向下的直角三角
#!/usr/bin/env python3
n = int(input('Enter the number of rows:'))
i = n
while i > 0:
x = '*' * i
y = " " * (n - i)
print(y + x)
i -= 1
  • 菱形
#!/usr/bin/env python3
n = int(input('Enter the number of rows:'))
i = 1
while i < n:
x = '*' * (2 * i - 1)
y = " " * (n - i)
print(y + x)
i += 1
while i > 0:
x = " " * (n - i)
y = '*' * (2 * i - 1)
print(x + y)
i -= 1

5、棍子游戏

有21根棍子,用户选1-4根棍子,然后电脑选1-4根棍子。谁选到最后一根棍子谁就输。(用户和电脑一轮选的棍子总数只能是5)

#!/usr/bin/env python3
sticks = 21 while True:
print("Sticks left: ", sticks)
sticks_token = int(input("Take sticks(1-4):"))
if sticks == 1:
print("Failed!")
break
if sticks_token >= 5 or sticks_token <= 0:
print("Choose wrong number! continue:")
continue
print("computer took:", 5-sticks_token, "\n")
sticks -= 5

注:结果是必输无疑,哈哈!

最新文章

  1. sessionStorage 和 localStorage 、cookie
  2. JavaScript、tabel切换完整版—自动切换—鼠标移入停止-移开运行
  3. 深入.net(多态二)
  4. JS日历制作获取时间
  5. PHP 使用 OSS 批量上传图片
  6. UINavigationBar 总结
  7. GoogleApis 屏蔽
  8. 重构第4天:降低方法(Push Down Method)
  9. AO创建IFeature的两种方法
  10. extern外部方法使用C#简单样例
  11. jQuery.fn.serialize 阅读
  12. HDU 1969(二分法)
  13. CodeForces 749D Leaving Auction
  14. Codeforces Round #354 (Div. 2)_Vasya and String(尺取法)
  15. 不同浏览器的margin值与padding值
  16. bzoj1193: [HNOI2006]马步距离
  17. Go a lot of way but I go back to the original point
  18. 2017-2018-1 1623 bug终结者 冲刺001
  19. 【机器学习】--线性回归中L1正则和L2正则
  20. 解决关于:TypeError: Class constructor Model cannot be invoked without &#39;new&#39;

热门文章

  1. SQL查询性能优化
  2. mysql动态执行sql批量删除数据
  3. 运维是做什么的?史上最全互联网Linux工作规划!十分钟找到linux运维工程师职业方向!
  4. php第十九节课
  5. C# 通俗说 内存的理解
  6. 【codeforces 701C】They Are Everywhere
  7. [HZOI 2016]tree—增强版
  8. [HZOJ10420]计算
  9. 爬虫——response中获取的不带主域名的url的拼接
  10. nyoj_85_有趣的数_201312122130