Ask coding questions

← Back to all posts
what is wrong in code
rocky321 (0)

https://replit.com/join/vckdsjrtlv-rocky321..htis my replit for day4 for who is going to pay bill.what is wrong in my code that i have return

Comments
hotnewtop
InvisibleOne (3186)

I don't see anything wrong with the code, you just need to have a comma and a space between the names.
Also, you don't need to get a random int and use that as the index for your list, you can just use random.choice(list) to get a random item from a list.
Shorten your code down to:

import random

names_string = input("Give me everybodies names, separated by a coma. \n")
names = names_string.split(", ") # this is splitting by a coma and a space

name = random.choice(names)

print(name + ' will pay the bill.')
rocky321 (0)

@InvisibleOne

random.choice we can't use,see the result in replit,
on putting name in input cheak for len function it give 1 as ans but ir should be 3 if input name are 3

InvisibleOne (3186)

Ok, then just do this:

import random

# Split string method
names_string = input("Give me everybody's names, separated by a comma. ")
names =names_string.split(", ")
# 🚨 Don't change the code above 👆

#Write your code below this line 👇


max = len(names) - 1

person = names[random.randint(0, max)]


print(f"{person} is going to buy the meal today!")

@rocky321

Coder100 (18818)

This is the error:

you need to make a space so it is a valid sentence!
You currently have Coder100will pay money
but it should be Coder100 will pay money

so the correct code is:

print(c + " will pay money")