码迷,mamicode.com
首页 > 微信 > 详细

Python实践:猜数字小程序Collatz序列

时间:2018-09-17 00:00:51      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:and   NPU   rand   def   turn   alt   ESS   input   you   

猜数字

  • 代码
‘‘‘
猜数字小游戏,不断输入你所猜的数(1-100),程序会根据你的输入提醒你进行
适当调整所猜数的大小,直到最后猜出这个随机数
‘‘‘
guessNumber = random.randint(1,100)
print("I‘m thinking a number between 1 and 100.")

while True:
    print(‘Take a guess.‘)

    guess = int(input())

    if guess > guessNumber:
        print("It‘s too high")
    elif guess < guessNumber:
        print("It‘s to low")
    else:
        print("Congratulations, you win.")
        break
  • 结果
    技术分享图片

Collatz序列

  • 代码
#Collatz序列
def collatz(number):
    if (number % 2 == 0):
        return number / 2
    else:
        return number * 3 + 1

print(‘Input a number.‘)

while True:
    global number1
    try:
        number1 = int(input())
    except ValueError:
        print(‘Please input a number‘)
        continue
    if collatz(number1) != 1:
        print(int(collatz(number1)))
    else:
        print(int(collatz(number1)))
        break
  • 结果
    技术分享图片

Python实践:猜数字小程序Collatz序列

标签:and   NPU   rand   def   turn   alt   ESS   input   you   

原文地址:http://blog.51cto.com/13473568/2175818

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!