python scrabble score calculation [closed] - python

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 3 years ago.
Improve this question
I am a beginner to python and I'm trying to make a simple scrabble score calculation program. May I know how to get the letter score from the letterScore function and add it to the scrabbleScore function? Thanks a lot for your help! Please have a look on the screenshot for the program I have tried~

That looks good, however I would recommend using a dictionary to get the letter scores:
values = { ('a':1), ('b':3), ('c':3), ... , ('z':10) }
then you can find the score much faster by using values[letter] inside your letterScore function.
Now to get the score from your function, you need to call the following inside the bottom for loop, above the totalScore calculation
score = letterScore(letter)
Hope this helps

I think you just need to move score = letterScore(letter) to the inside of your loop in scrabbleScore

Related

Trying to predict the random.shuffle result in Python [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 last month.
Improve this question
For a class projet, i want to be able to predict the result of a random.shuffle, but what i learned about the random module is way more advance than everything i learned so far.
Do you have any idea of how it could be done ?
I found some code that allowed me to predict the result of randint, but nothing for shuffle
The following code should always give the same output:
lst = [0,1,2,3,4]
random.seed(5)
random.shuffle(lst)
print(lst)
My guess is that if you managed to make it work for other methods, you simply forgot to reinitialize the list each time

python design choice - if condition inside loop that is run for many iterations [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I have a for loop like -
for x in range(int(1e9)):
#lot of code
Now based on user input, I need to make a small change to the code inside this loop like this.
if user_input == 'hi':
for x in range(int(1e9)):
#small change
#lot of code
else:
for x in range(int(1e9)):
#lot of code
However, lot of code is being repeated unnecessarily, creating redundancy.
One option is to have the if conditioninside the for loop. But this would increase the execution time considerably due to many iterations (have timed it and verified).
Another option I thought of is creating a function which does lot of code. However, lot of code contains a lot of variables present outside this for loop. This function would then have many many parameters, which I do not want.
What is the best way to organize this code? Thanks!
You can declare a function that uses **kwargs instead of declaring all the parameters, this will give you a dictionary with all the parameters as keys
def func(**kwargs):
print(kwargs['param1']) # 1
print(kwargs['param2']) # asd
func(param1=1, param2='asd')

About web scraping in python [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 4 years ago.
Improve this question
There are two major variables (calls and puts), and several sub-variables (e.g. bid, change, time etc.) For example, if there are total 5 data points. I know how to do separately:
data[u'options'][0]["calls"][0]["change"]['fmt'], data[u'options'][0]["calls"][1]["change"]['fmt'], data[u'options'][0]["calls"][2]["change"]['fmt'], data[u'options'][0]["calls"][3]["change"]['fmt'],data[u'options'][0]["calls"][4]["change"]['fmt']
but that spend too much time. I wonder how to choose multiple items in one code.
You can do this with a little bit of list comprehension if I understand your question properly.
For each value in data["options"][0]["calls"], it adds that value's ["change"]["fmt"] value to the list.
d = [call["change"]["fmt"] for call in data["options"][0]["calls"]]
If you want a list of EACH value from every set of options, you could do it like so:
d = [[call["change"]["fmt"] for call in option["calls"]] for option in data["options"]]
and now you can say
for option in d:
for call in option:
print(call)
[data[u'options'][0]["calls"][i]["change"]['fmt'] for i in range(5)]
I don't quite understand your problem, is this what you're after?

Convert < > from string [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 4 years ago.
Improve this question
I want to get < from a string that contains the character.
So what I want to do is:
magic_function('<') = <
in order to make. For example:
1 magic_function('<') 3
I expect, of course, that the program returns True
This isn’t possible with specifically what you want. A function returns an object, not an operator. I’m not sure what your main intention is here but you might want to rethink your architecture.
What you can do (but what I don’t recommend) is using eval() like so:
eval(“1 < 3”)
But there are many resources online about why eval is “evil”: to sum them up, if you don’t know what your data source is, then you could be performing unexpected operations which you might not want.

Read each line in a text file, store the values in a list, and compute scores [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Read each line in a text file, store the values in a list, and compute scores
The best approach this problem is to list all the functions you need to do the task. A typical example is:
Read file.
Read list.
Take list and get each string delimited by space.
Store string into an array.
...
Then go to the Python website and lookup how to do each function.
Example: To do input and output function in python:
https://docs.python.org/2/tutorial/inputoutput.html
Also, you can look up function by asking google. Google will then point you to answers to your questions:
Q: How to find mean of a list?
A: Finding the average of a list
If you do this enough times, eventually you will be able to write a problem that solves the problem listed.
Good Luck!

Categories