简体   繁体   中英

When I want to randomly assign a role by doing random.shuffle i get this error: TypeError: can only concatenate str (not "NoneType") to str

I have recently begun working on a small program and I wanted to use the random.shuffle way of giving someone a random role. Everything works great up until I reach the part where the role is actually mentioned.

import random
roles = ['Lizard','Human']

random.shuffle(roles)

name = input("Enter Your Name")

color = input("Select a color \n Blue \n Red \n Green \n Yellow \n Color: ")

print(name + " You are " + random.shuffle(roles))

First of all, you can't expect to get a returned value from a processed object. This line will give you an error because you expect a returned value from None input print(name + " You are " + random.shuffle(roles)) .

So, you need to understand how random.shuffle works. random.shuffle is a function that takes a list and swap its values, randomly. So, what you need to do is only one thing which declares the role after you executed--> called the random.shuffle function.

So, It has to go like that :-

import random
roles = ['Lizard','Human']

random.shuffle(roles) # called function to swap the values of the roles _list

name = input("Enter Your Name")

color = input("Select a color \n Blue \n Red \n Green \n Yellow \n Color: ").upper() # upper() function for giving the user to write whatever he want in case you want to use this input for any other later benefits...

print(name + " You are " + roles[0]) # declaring the first value in the list doesn't mean it will be 'lizard' after randomly swapping it may display as 'human' or 'lizard'

Examples of OUTPUT

# Output No. 1: -

Enter Your Name Jhon
Select a color 
 Blue 
 Red 
 Green 
 Yellow 
 Color: Blue
 Jhon You are Human

# Output No. 2

Enter Your Name Slave
Select a color 
 Blue 
 Red 
 Green 
 Yellow 
 Color: green
 Slave You are Lizard

The problem here is that random.shuffle shuffles the list in-place , but the function's return value is not the shuffled list, but actually None .

As an alternative, the function random.choice randomly chooses an element from its argument. I think this is what you'd like to use. Your code should then look like:

import random

roles = ['Lizard', 'Human']

name = input("Enter Your Name")
color = input("Select a color \n Blue \n Red \n Green \n Yellow \n Color: ")

print(name + ", you are a " + random.choice(roles))

Note that there is no need to shuffle the roles list beforehand.

I have recently begun working on a small program and I wanted to use the random.shuffle way of giving someone a random role. Everything works great up until I reach the part where the role is actually mentioned.

import random
roles = ['Lizard','Human']

random.shuffle(roles)

name = input("Enter Your Name")

color = input("Select a color \n Blue \n Red \n Green \n Yellow \n Color: ")

print(name + " You are " + random.shuffle(roles))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM