我是一个全新的程序员,从我所读到的Python很容易学习,所以我尝试学习它。这只是一个有趣的脚本,我在几分钟内,我想知道我是否可以缩短它。如果你不知道,这基本上只是让用户输入三个变量,然后选择其中一个,重复三次,然后组合答案。你知道吗
import random
import time
print("name three diffrent animals")
animal1 = input("1")
animal2 = input("2")
animal3 = input("3")
x = (random.randint(1,3))
if x == 1:
x = animal1
if x == 2:
x = animal2
if x == 3:
x = animal3
print("name three diffrent colors")
color1 = input("1")
color2 = input("2")
color3 = input("3")
y = (random.randint(1,3))
if y == 1:
y = color1
if y == 2:
y = color2
if y == 3:
y = color3
print("name three diffrent sports")
sport1 = input("1")
sport2 = input("2")
sport3 = input("3")
z = (random.randint(1,3))
if z == 1:
z = sport1
if z == 2:
z = sport2
if z == 3:
z = sport3
print("your dream animal is a.....")
time.sleep(3)
print(y, ',' , z, 'playing', x,'!')
这里有一个建议
用包装开箱怎么样?你知道吗
用
choice()
代替randint()
?你知道吗以及(可能)使用f字符串进行打印?你知道吗
相关问题 更多 >
编程相关推荐