Python实践:猜数字小程序Collatz序列
时间:2018-09-17 00:00:51
收藏:0
阅读:249
猜数字
- 代码
‘‘‘
猜数字小游戏,不断输入你所猜的数(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
- 结果
评论(0)