猜数字
- 代码
‘‘‘
猜数字小游戏,不断输入你所猜的数(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
- 结果
原文地址:http://blog.51cto.com/13473568/2175818
时间: 2024-11-08 12:59:12