For fun I'm trying to create a character generator sheet for Dungeons and Dragons. I've got the program to randomly roll for my strength, charisma etc.
Now I want to be able to ask the user, "What type of weapon do you want to use?" Get_Weapon_Choice, then pull up a list of that weapon type. I've tried creating a list weapon_Choices = ['Bow', 'Sword', ]. Then I created 2 other lists, bow = ['short', 'long', 'crossbow'] and swords = ['short', 'long'] I know how to get input from the user, but I don't know how to take that input and print the list, I'm printing the variable name.
chooseWeapon = input('What type of weapon would you like? Bow or Sword?')
How do I use chooseWeapon to compare to weapon_Choices to make sure they didn't enter something like Spells, then use chooseWeapon to print either the bow[] list or the swords[] list?
Do I need to use MySQL along with Python? and create tables and then search the tables instead of lists?
You can create a dictionary:
weapons = {"bow": ['short', 'long', 'crossbow'], "sword": ['short', 'long']}
# prompt user and convert to lowercase (our dict consists of lowercase strings)
chooseWeapon = input('What type of weapon would you like? Bow or Sword?').lower()
if chooseWeapon in weapons: # checks if the input is one of the keys in our dict
print(f'Available {chooseWeapon}s: {weapons[chooseWeapon]}')
Related
I am new to python and my coding experience so far is with MATLAB.
I am trying to understand more about lists and dictionaries as i am using a library about DOEs that takes an dictionary as a passing argument.
But my trouble so far is that this dictionary assumes the form of ex.
DOE={'Elastic Modulus':[10,20,30], 'Density':[1,2,3], 'Thickness':[2,3,5]}
But i need this dictionary to be user defined, for example:
Have an input to define how many variables are needed (in this example are 3: Elastic Modulus','Density'and 'Thickness)
as the variables are defined, it should be able to store values in the dictionary over a for loop.
Is this possible using dictionaries?
Or is it better to use a list and convert in a dicionary later?
Thank you in advance
One can add keys and the corresponding values to a dict one at a time like so:
my_dict = {}
num_entries = int(input("How many entries "))
for _ in range(num_entries):
key = input("Enter the key: ")
value = input("Enter the value: ")
my_dict[key] = value
Presumably you would have a loop to do the entry of key and value for the number of values you wish to enter. Also if you are in python 2 it needs to be raw_input rather than input function. [Edit: Showing how to do the loop, since I noticed that was part of your question]
I'm a noob, working on making the Yahtzee game I programmed multiplayer. In Python 2.7, I want to have a prompt where a user can enter the number of players, i.e. 2, 3,4, 2007 etc., after which for the number of players entered, i.e. 3, the user will enter the names of the players, i.e. Mike, Tom, Jim, which I can then use in my program to keep score (i.e. Mike's score is 7, he's pretty bad, Jim has 250, he's pretty good etc.). I've seen suggestions to use dictionaries, classes and arrays, but I'm lost as to which one is best, and worst still, can't make what I'm trying to do work.
from collections import defaultdict
d = defaultdict(int)
d = {}
players = raw_input('How many players?')
players = int(players)
for i in range (1,players+1):
d = raw_input('Enter player name')
print d
my code on Repl.it is here
In your for loop you are assigning whatever the player types in as a name to be equal to d. d is therefore instantly no longer referring to any dictionary, but a string.. whatever the name was. A particular variable can only refer to one object at a time. Logically this is straight-forward if you think about what happens when you re-assign a variable to something new, how could the interpreter possibly differentiate between multiple possible objects when all it's provided is the label d..
Try something like this, a dict seems good to me:
players_dict = {}
num_players = raw_input("Enter number of players: ")
for i in num_players:
name = raw_input("Enter name for player %s:" % i)
# set initial score to 0
players_dict[name] = 0
Thanks all for the help. With your help, I figured out the following code (using a list and appending to it), which worked:
player_names = []
input_players = int(raw_input('how many players?'))
for i in range(0,input_players):
name = raw_input('enter player name')
name = name.upper()
player_names.append(name)
#using my new player names to iterate through turns:
for i in range(0,13):
for i in player_names:
print i # placeholder for my turn function
I'd like to know how to define this as a list within Haskell so that I could perform operations such as tail, reverse and length:
cars = "lamborghinis are my favourite types of car"
I've tried:
let cars = [lamborghinis,are,my,favourite,types,of,car]
let cars = ["lamborghinis","are","my","favourite","types","of","car"]
let cars = ['lamborghinis','are','my','favourite','types','of','car']
I have been using http://learnyouahaskell.com/starting-out as a tutorial as I am new to Haskell and I can't see where I'm going wrong, I thought my first attempt above would be correct as that it how it does it in the tutorial but with numbers instead of words.
The error I am getting is: parse error on input 'of.
Any ideas where I'm going wrong? Thanks.
let cars = "lamborghinis are my favourite types of car"
makes cars a list of characters [Char]==String and head cars should give you the first letter. This is special syntax of haskell for strings
let cars2 = ["lamborghinis","are","my","favourite","types","of","car"]
This is the normal form for defining lists and gives you a list of strings [String] and head cars should give you "lamborghinis".
You can also split a sentance into words using the words function.
let cars3 = words cars
The type String is only a type synonym for [Char].
This means that for example "Hello, you" is the same as ['H','e','l','l','o',',',' ','y','o','u']
I'm writing an application that allows the user to configure the output using templates. For example:
Variables:
name = "BoppreH"
language = "Python"
Template:
My name is {name} and I like {language}.
Output:
My name is BoppreH and I like Python.
This works fine for simple data, like strings and numbers, but I can't find a good syntax for lists, more specifically for their delimiters.
fruits = ["banana", "apple", "watermelon"]
I like {???}.
I like banana, apple, watermelon.
In this case the desired delimiter was a comma, but how can the user specify that? Is there some template format with this feature?
I'm more concerned about making the syntax simple to understand, regardless of language or library.
Implement filters, and require their use for non-scalar types.
I like {fruits|join:", "}.
Typically, a list contains an unknown number of members, and sometimes variables/placeholders of its own. An example might be listing the name of a person along with their phone numbers. The desired output might look something like this:
John Doe
555-1212
555-1234
In a templating system that supported this, you'd need two types of variables: One that designated a placeholder for a value (like the curly braces you're using now), and another to denote the start and end of the list. So, something like this:
{name}
{{phone_numbers}}{phone}{{/phone_numbers}}
Your array of values might look like this:
values = [names: "John Doe", phone_numbers: [ phone: "555-1212", phone: "555-1234"]]
Each value in the "phone_numbers" array would create a new instance of everything that existed between {{phone_numbers}} and {{/phone_numbers}}, placing the values contained within those two "tags".
i have a yml file that reads like this
RealBank:
Players:
player1:
Balance: '0'
player2:
Balance: '0'
player3:
Balance: 63050
i can get python to print out each player name. but for the life of me i cannot take that playername, and get their balance to print up.
my code looks like this:
stream = open("config.yml", "r")
doc = yaml.load(stream)
beans = doc['RealBank']['Players']
for bean in beans:
print bean
print doc['RealBank']['Players']['%s']['Balance'] % bean #this command does not work, no matter how i try to assemble it.
this gives me a list of each players name in the yaml file. but i've tried everything i can think of and search for to try and make it so i can take that players name and insert it into another search for their balance.
i know its probably something super simple that im probably overlooking and just not thinking of the right phrase to google and will probably be facepalming half an hour once someone points it out.
doc consists of just plain Python builtin types. You're not really querying anything.
That line that isn't working is trying to format the result of doc['RealBank']['Players']['%s']['Balance'], with %s being the literal key name. If you want to use bean as a key, then just pass in the variable:
print doc['RealBank']['Players'][bean]['Balance']
You could also use .items() to iterate over key, value tuples:
for name, player in doc['RealBank']['Players'].items():
print name, player['Balance']