HELP PLEASE printing single characters!
hi all, I have been trying to figure out all day why my code is printing single characters from my list when i print random elements using random.choice the elements in the list are not single characters for example when i print, print(LIST[random.choice]) i get: ["e", "x", "a", "m", "p", "l", "e"] when i should get ["example"]. my code is: #Create a program that prints a list of words in random order. #The program should print all the words and not repeat any. import random LIST = ["blue ", "red ", "yellow ", "green ", "orange "] order = [] print("This game will print a random order of colours") print("The list is", LIST) input("press enter to start") while LIST != []: choice = random.choice(LIST) order += choice while choice in LIST: LIST.remove(choice) print(order) input("press enter to exit") thanks in advance guys -- https://mail.python.org/mailman/listinfo/python-list
Re: HELP PLEASE printing single characters!
On Wednesday, December 2, 2015 at 7:09:23 PM UTC, Ian wrote: > On Wed, Dec 2, 2015 at 12:58 PM, Dylan Riley wrote: > > hi all, > > I have been trying to figure out all day why my code is printing single > > characters from my list when i print random elements using random.choice > > the elements in the list are not single characters for example when i > > print, print(LIST[random.choice]) i get: > > ["e", "x", "a", "m", "p", "l", "e"] when i should get ["example"]. > > Remember that strings are iterable, and that iterating over strings > results in individual characters. That should give you a clue as to > what's going on. > > > my code is: > > #Create a program that prints a list of words in random order. > > #The program should print all the words and not repeat any. > > > > import random > > > > LIST = ["blue ", "red ", "yellow ", "green ", "orange "] > > order = [] > > > > print("This game will print a random order of colours") > > print("The list is", LIST) > > input("press enter to start") > > > > > > > > while LIST != []: > > choice = random.choice(LIST) > > order += choice > > Addition on a list does concatenation, not appending. So this takes > each element from choice and adds them individually to order. hi ian what would be the correct code to use in this situation then because as far as i am aware the elements of my list should be printed as whole elements and not just characters of the elements. -- https://mail.python.org/mailman/listinfo/python-list
error help import random
This is my fortune cookie program i wrote in python. the problem is it will not run past the first line of input. could someone please identify the error and explain to me why. here is the code: #the program silulates a fortune cookie #the program should display one of five unique fortunes, at randon, each time its run import random print(" \\ \\ \\ \\ DYLANS FORTUNE COOKIE \\ \\ \\ ") print("\n\n\tGood things come to those who wait") input("\nPress enter to see your fortune") fortune = random.randrange(6) + 1 print(fortune) if fortune == 1: print("happy") elif fortune == 2: print("horny") elif fortune == 3: print("messy") elif fortune == 4: print("sad") elif fortune == 5: print("lool") print("hope ypu enjoyed your fortune being told") input("\nPress enter to exit") many thanks in advance -- https://mail.python.org/mailman/listinfo/python-list
Re: error help import random
On Friday, November 20, 2015 at 11:06:05 PM UTC, Rob Gaddi wrote: > On Fri, 20 Nov 2015 15:15:42 -0500, Terry Reedy wrote: > > > On 11/20/2015 12:22 PM, Dylan Riley wrote: > >> This is my fortune cookie program i wrote in python. > >> the problem is it will not run past the first line of input. > >> could someone please identify the error and explain to me why. > >> here is the code: > >> > >> #the program silulates a fortune cookie #the program should display one > >> of five unique fortunes, at randon, each time its run > >> > >> import random > >> > >> print(" \\ \\ \\ \\ DYLANS FORTUNE COOKIE \\ \\ \\ ") > >> print("\n\n\tGood things come to those who wait") > >> input("\nPress enter to see your fortune") > >> > >> fortune = random.randrange(6) + 1 print(fortune) > >> if fortune == 1: > >> print("happy") > >> elif fortune == 2: > >> print("horny") > >> elif fortune == 3: > >> print("messy") > >> elif fortune == 4: > >> print("sad") > >> elif fortune == 5: > >> print("lool") > > > > Use a dict instead of if-elif. > > > > i = random.randrange(6) > > fortunes = {0:'happy', 1:'horny', 2:'messy', > > 3:'sad', 4:'lool', 5:'buggy'} > > print(i, fortunes[i]) > > > > Or a list/tuple, which saves in typing, memory, and access time. > > fortunes = ('happy', 'horny', 'messy', 'sad', 'lool', 'buggy') > i = random.randrange(len(fortunes)) > print(i, fortunes[i]) > > Or to be lazier still, just use random.choice > > fortunes = ('happy', 'horny', 'messy', 'sad', 'lool', 'buggy') > print(random.choice(fortunes)) > > None of which actually addresses the OP's issue with input(), but it's > nice to get the back half clean as well. > > -- > Rob Gaddi, Highland Technology -- www.highlandtechnology.com > Email address domain is currently out of order. See above to fix. very nice i like the way you code. i managed to solve the problem it was because i wasnt using raw_input however i updated to python 3 so the original now works. many thanks anyway -- https://mail.python.org/mailman/listinfo/python-list
anyone tell me why my program will not run?
i am learning python and was tasked with making a program that flips a coin 100 times and then tells you the number of heads and tails. I have done so coming up with this piece of work but it doesnt run can anyone help me out? #This is credited to dylan print(" \\ \\ \\ \\ \\ \\ \\ \\ D FLIPS \\ \\ \\ \\ \\ \\ \\ \\") print("\n\nThis is D's coin flipper program. You get 100 flips. \n\t LETS SEE HOW LUCKY YOU ARE") input("Press enter") import random heads = int("1") tails = int("2") flips = 100 headscount = 0 tailscount = 0 while flips != 0: flips -= 1 result = random.randint(heads, tails) if result = heads: headscount += 1 else: tailscount += 1 print(headscount, tailscount) input("press enter to exit") -- https://mail.python.org/mailman/listinfo/python-list