Reputation: 3
I am a brand new programmer, and from what I have read Python is easy to learn so I tried learning it. This is just a fun script I made in a few minutes and I was wondering if I could shorten it. In case you can't tell, this essentially just has the user input three variables, then picks one of them, repeat three times, and then combines the answers.
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,'!')
Upvotes: 0
Views: 65
Reputation: 719616
Here's a suggestion
# read a list of N animal names
animals = list(map(lambda i: input(str(i)), range(1, N)))
# read a small (fixed) number of animal names
animals = input("1"), input("2"), input("3")
# select a random animal from the list
x = animals[random.randint(0, len(animals) - 1)]
# or
x = random.choice(animals)
Upvotes: 1
Reputation: 4141
How about using packing-unpacking?
print("Name three different animals: ")
animals = input("1: "), input("2: "), input("3: ")
And using choice()
instead of randint()
?
x = random.choice(animals)
And (maybe) using f string for printing?
print(f"{y} {z} playing {x}!")
Upvotes: 1