Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help solving this
#1
[Image: BRzRZMR]

Hi ,rather new at python and need help solving this

It would seems that i am not able to generate more than 2 dishes even though i input 2 days
( Click on image to see codes )

Many thanks for your help
Reply
#2
First of all - you should post code not images.

Second - why should you get two dishes if you don't use argument value in the body of the function?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
try using below to see if that helps,

from random import choice
def choosedishes(days):
	count=0
	while count < days:
		chosendish=choice(foodwelike)
		if chosendish not in mymenu:
			mymenu.append(chosendish)
			count += 1
		#elif chosendish in mymenu:
			#mymenu.append('repeat item -> '+chosendish)
			#count += 1
foodwelike=['food a','food b','food c','food d','food e','food f','food g']
mymenu=[]

print("hai, i will help you plan dinner menu..")
answer=input("how many days would you like me to plan?  ")
print(f"Alright, i am going to plan {answer} dinner(s) from our menu lists")

choosedishes(int(answer))
print(mymenu)
Best Regards,
Sandeep

GANGA SANDEEP KUMAR
Reply
#4
I don't understand: do you need random choice with replacement or without replacement?

Without replacement (unique choices):

>>> food_we_like=['spam', 'ham', 'eggs', 'bacon']                    
>>> random.sample(food_we_like, 2)                                   
['ham', 'bacon']
>>> random.sample(food_we_like, 5)          # list has 4 items, we want 5                                   
/.../
ValueError: Sample larger than population or is negative
With replacement (choices may repeat):

>>> random.choices(food_we_like, k=2)                                
['eggs', 'eggs']
>>> random.choices(food_we_like, k=5)       # we can have 5 choices from list of 4 items                              
['bacon', 'spam', 'eggs', 'spam', 'ham']
As one can see - it's easy to pass number of choices into both way of selection.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Forum Jump:

User Panel Messages

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