Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello anyone could help me with this question I have been struggling with?


Create a random name generator for a new pet! Write a function that asks the user for 6 names that will be saved in a list. Then, write another function that randomly choose one of those names from the list. Then, in the main() function, print the name that was chosen. You must use the random library to achieve this.

What I have tried:

Python
import random

def choose_random(temp_list):
return temp_list[random.randint(0,5)]

def input_function():
lst=[]
print("Enter Six Names: ")
for i in range(0,6):
name=str(input())
lst.append(name)
return choose_random(lst)

def main():
print("The chosen name of the dog is: "+input_function())

if __name__ == "__main__":
main()
Posted
Updated 20-May-21 8:32am
v2
Comments
Richard MacCutchan 21-May-21 5:51am    
With correct indentation, your code works fine.

1 solution

Start by learning Python: it is an unusual language in that indentation is significant - it determines what code is part of what.
So when you create a function, all the code in the body of the function must be indented to the same level (or greater) and that level of indentation cannot be zero.
So this makes a two line function:
Python
def foo():
   line1
   line2
line3
In your code, none of it is indented, so nobody can tell what is supposed to be part of the function and what isn't.

So fix that, then try again to see how well it works.
 
Share this answer
 
Comments
CPallini 21-May-21 2:02am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900