random in python - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: random in python (/thread-33851.html) |
random in python - ilikedofs - Jun-02-2021 I seem to have a problem. When I use random.choice it will print out the list instead. here is the code and the outputfirst the code print('Welcome to the fart monster game') print('----------------------------------') user=input('What is your name ') print("ok",user,"here are the controls: g(grab),l(left),r(right),u(up),d(down)") import random rooms= ['bedroom','dungeon','The Motionless Maze','kitchen','bathroom','office'] random.choice(rooms) start=input('press n to start') if start =='n': print('You are thrown into the',rooms,) elif start != ('n'): print('that is invalid') lifes=10and output And also if you see any problems with the code please let me know
RE: random in python - deanhystad - Jun-02-2021 Choice is a function that returns an item randomly chosen from the list. You ignore the return value RE: random in python - BashBedlam - Jun-02-2021 (Jun-02-2021, 12:35 AM)deanhystad Wrote: Choice is a function that returns an item randomly chosen from the list. So here's you you would use the returned value from the random.choice function: print('Welcome to the fart monster game') print('----------------------------------') user=input('What is your name ') print("ok",user,"here are the controls: g(grab),l(left),r(right),u(up),d(down)") import random rooms= ['bedroom','dungeon','The Motionless Maze','kitchen','bathroom','office'] random_room = random.choice(rooms) # variable is assigned the return value start=input('press n to start') if start =='n': print('You are thrown into the',random_room) elif start != ('n'): print('that is invalid') lifes=10 RE: random in python - menator01 - Jun-03-2021 I think I would use some list variables in case I wanted to access the rooms variable again. Maybe something like: #! /usr/bin/env py from random import choice # Set some variables welcome_string = 'Welcome to the Fart Monster Game' control_list = ['g(grab)', 'l(left)', 'r(right)', 'u(up)', 'd(down)'] rooms = ['bedroom', 'dungeon', 'The Motionless Maze', 'kitchen', 'bathroom', 'office'] print(f'{welcome_string}') print(f'-' * len(welcome_string)) user = input('What is your name? \n>>') print(f'Ok, {user.capitalize()}, here are the controls: {", ".join(control_list)}') start = input('press s then enter to start\n>>') if start == 's': print(f'You are thrown into the {choice(rooms)}') else: print('invalid') |