Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
random in python
#1
Question 
I seem to have a problem. When I use
random.choice it will print out the list instead. here is the code and the output
first 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=10
and output
Output:
Welcome to the fart monster game ---------------------------------- What is your name d ok d here are the controls: g(grab),l(left),r(right),u(up),d(down) press n to startn You are thrown into the ['bedroom', 'dungeon', 'The Motionless Maze', 'kitchen', 'bathroom', 'office']
And also if you see any problems with the code please let me know
Reply
#2
Choice is a function that returns an item randomly chosen from the list. You ignore the return value
Reply
#3
(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
Reply
#4
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')
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020