List Object Not Callable, Syntax Error for Text-Based RPG

2024/11/16 14:28:35

I'm having issues (still) with generating a random object, in this case a random herb found by foraging. Here's the code for the function:

def collectPlants(self):if self.state == 'normal':print"%s spends an hour looking for medicinal plants." % self.nameif random.choice([0,1]):foundHerb = random.choice(herb_dict)print "You find some %s." % foundHerbreturn random.choice(herb_dict)else: print"%s doesn't find anything useful." % self.name

and the dict block:

herb_dict = [("Aloe Vera", Player().health == Player().health + 2),("Cannabis", Player().state == 'high'),("Ergot", Player().state == 'tripping')
]

Sorry for the clandestine examples. Herb is also a class with three parameters: (self, name, effect).

How do I generate a random herb from the dict when the collectPlants function is called?

Answer

random.choice is a method so if you want to call it you need to use () instead of []. Also to access an element in the list you need to use [] not ().

And for your case, replace the return random.choice[herb_dict("")] with return random.choice(herb_dict) can meet you need and it will randomly return an element in herb_dict.

But I think the logic of you method has some problem.

print "You find some %s." % herb.name
player.hasHerb()

The above two statements will never be executed.

Also you can use if random.choice([0, 1]): instead of if random.randint(0,1) == 1:.

https://en.xdnf.cn/q/119182.html

Related Q&A

Why does my Tkinter window background not change?

Ive a small program with a feature to change the background color of a different window than the frame I use to ask for the background color. (I hope you understand this.) The program is written in Pyt…

Combining a nested list without affecting the key and value direction in python

I have a program that stores data in a list. The current and desired output is in the format:# Current Input [{Devices: [laptops, tablets],ParentCategory: [computers, computers]},{Devices: [touch], Par…

Splitting very large csv files into smaller files

Is Dask proper to read large csv files in parallel and split them into multiple smaller files?

How to make this Python script to run subfolders too?

Which part of the codes do I need to change in order to include subfolders? File handle.py import glob import os import sys from typing import Listdef get_filenames(filepath: str, pattern: str) -> …

Why doesnt this recursive GCD function work? [duplicate]

This question already has answers here:Why does my recursive function return None?(4 answers)What is the purpose of the return statement? How is it different from printing?(15 answers)Closed 1 year …

How can I append \n at the end of the list in list comperhansion

I have this code:def table(h, w):table = [[. for i in range(w)] for j in range(h)]return tablewhich returns this[ [., ., .], [., ., .], [., ., .], [., ., .] ]How to make it return this?[[., ., .],[., …

Seaborn bar plot y axis has different values than expected

The y values in the Seaborn barplot are different than what my data shows.My data shows:yearmonth 2018-10 763308.0 2018-11 708366.0 2018-12 703952.0 2019-01 844039.0 2019-02 749583.…

Merge multiple JSON into single one (Python)

I am searching for a way to merge multiple JSONs into a single one. My output is in this format:[{"Nome bollettino": "Bollettino 1"}, {"Causale": "1"}, {"Nu…

I need to filter contents of my text file

I have a text file that I want to loop through, slice some contents, and store in a separate list. The text file contains:blu sre before we start start the process blah blah blah blha end the process b…

How to remove a number from a list that has a range between two numbers? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 6 years ago.Improve…