R
R
Rav2018-04-30 07:16:35
Python
Rav, 2018-04-30 07:16:35

Absolute noob. Which built-in functions to use for a basic task in Python?

I'm taking my first steps in learning programming. Started a couple of weeks ago.
The task is as follows: you need to ask the user to enter the name of the animal and the numbers. Using the number entered by the user, ask the user to enter the same number of colors, each on a separate line. After the user completes the input of colors, the program should display the strings with the name of the colors entered by the user.
It looks something like this (user input is in bold):
Enter an animal: cat
How many colors: 4
Color 1: white
Color 2: red
Color 3: blue
Color 4: brown
white cat
red cat
blue cat
brown cat
I started like this:

colors = ["red", "blue", "black", "white", "yellow", "orange", "purple", "brown", "grey", "gray", "silver", "green"]
userAnimal = input("Enter an animal: ")
userNumber = input("How many colors: ")

userNumber = int(userNumber)

And then I got up completely :(
Share your knowledge. Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Animkim, 2018-04-30
@Animkim

It is strange, of course, that in two weeks you did not get to the cycles.

user_number = 4
user_colors = []
while len(user_colors) < user_number:
    user_colors.append(input("Color? "))

D
DDDsa, 2018-04-30
@DDDsa

Here is a solution to your problem:

animal = input('Enter an animal: ')

try:
    input_colors = input('How many colors? ')
    num_colors = int(input_colors)
except ValueError:
    print('%s is not a number' % input_colors)
    exit()

colors = []
for i in range(num_colors):
    colors.append(input('Color %s: ' % (i + 1)))

for color in colors:
    print('%s %s' % (color, animal))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question