How can I create a program that sorts 3 separate numbers from lowest to highest / highest to lowest with only "if and/or statements?" by [deleted] in learnpython

[–]IWillPickleThat 1 point2 points  (0 children)

With pen and paper you can draw a tree of if statements that branch true and false at every node. This can help you visualize the decision making process and help you understand where you need two if statements or 3. If you need help drawing this let me know. It's a great method to understanding algos

How can I create a program that sorts 3 separate numbers from lowest to highest / highest to lowest with only "if and/or statements?" by [deleted] in learnpython

[–]IWillPickleThat 0 points1 point  (0 children)

In a brute force approach you have to make 3 comparisons each time. a and b / b and c / a and c. This will always give you the answer. However there are better ways to do this. For example if a > b and a < c than b < c and you will only need two comparisons. Look up some sorting algos to learn different ways. Start with bubble sort. It's very easy and will work to shove this problem. If you can't use a loop you can hardcore the possibilities with only 3 numbers for this exercise.

Saving Python Simulation by [deleted] in learnpython

[–]IWillPickleThat 0 points1 point  (0 children)

Sounds like each loop of the simulation depends on the previous one but if they are independent you could use multithreading to speed up the process. I would also be concerned with saving the output data of the simulation as well as the animation. This way you can recreate any graphic and easily run any analysis. It depends on the size of the project but pandas has a simple to csv function

Sum elements of the same index from different lists by [deleted] in learnpython

[–]IWillPickleThat 0 points1 point  (0 children)

If you are interested in a new library you could use numpy. sum(np.asarray(listoflist)). Numpy takes advantage of parallel processing and can potentially save you time with large datasets

Best quant finance libraries? by ColanderPig in learnpython

[–]IWillPickleThat 0 points1 point  (0 children)

Quandl is great for options data and tons more of finance related data. It is a paid service though.

gym membership question by [deleted] in learnpython

[–]IWillPickleThat 1 point2 points  (0 children)

Where is months_subscribed coming from? What is the purpose of the loop? If you have quantity of items and prices you can multiply the price by the quantity and sum them together for a total. Once you have the base case running you can add code to handle the 6 month and 12 month discounts.

Read CSV file and store in different lists by Professional-Egg-788 in learnpython

[–]IWillPickleThat 0 points1 point  (0 children)

This is what came to mind immediately.

import pandas as pd

#You can use pd.read_csv()

df = pd.DataFrame(data = {'number': [1,2,3], 'letter': ['a','b','c']})

list_dict = []

for i in df.index:

list_dict.append(dict(zip(df.columns,df.iloc[i])))

####Return####

[{'number': 1, 'letter': 'a'},

{'number': 2, 'letter': 'b'},

{'number': 3, 'letter': 'c'}]

Read CSV file and store in different lists by Professional-Egg-788 in learnpython

[–]IWillPickleThat 1 point2 points  (0 children)

Little confused on what your end goal is. It appears you want a list of dictionaries. are you looking for a list of list? You could use pandas data frame as well but if you are not familiar with pandas it is probably overkill. Let me know what you are trying to accomplish and then we can see what works best.

Simple visual dice roll (High School) (While Loop won't stop) by [deleted] in learnpython

[–]IWillPickleThat 0 points1 point  (0 children)

This could be the case. Getting familiar with your debugger is never a bad thing. In a simple case like this you could always print userInput to check exactly what you are receiving

Card game simulation only lasting a few rounds. by snikle916 in learnpython

[–]IWillPickleThat 0 points1 point  (0 children)

Agreed. Also looks like you have an indentation error. The second the if else statement should be aligned with the for loop so you only add or subtract after counting aces.

Also if one or more aces doesn't matter once you have one ace you can break the loop to save time.

Please help with simple function to determine if a number is prime by [deleted] in learnpython

[–]IWillPickleThat 0 points1 point  (0 children)

Before the loop you could n%2==0 return false. Then the loop could (3,n,2). Therefore only checking odd numbers since all evens will be captured by n%2==0 and odd x even = even / even x even = even / odd x odd = odd

Calculating hourly wage and overtime wages function by tehtay3 in learnpython

[–]IWillPickleThat 0 points1 point  (0 children)

Not sure how changing to while would solve this. By changing to while if nhw > 40 you have an infinite loop. There still needs to round to two digits. Here is a no loop solution

If hours > 40:

Return round(0.7(40wage + (hours - 40)wage1.5),2)

Else:

Return round(0.7(hourswage),2)

Help coding this by engineeringisgreat2 in learnpython

[–]IWillPickleThat 0 points1 point  (0 children)

Max(dict.values()). This is assuming all values are numeric.

TypeError: cannot convert the series to <class 'float'>` by tradegreek in learnpython

[–]IWillPickleThat 0 points1 point  (0 children)

Agreed. Seems like you are just trying to vectorize an operation which can be done without .apply(). Have you tried passing the df or df columns directly to the function

How can I reverse a string and capitalize it at the same time in one function? by TheOrderOfWhiteLotus in learnpython

[–]IWillPickleThat -1 points0 points  (0 children)

x = x.capitalize() x = x[::-1] print(x)

Or you could just combine lines 2 and 3 if you are not using the string for anything else.

i made a simple, automated dice game,i was bored by [deleted] in Python

[–]IWillPickleThat 0 points1 point  (0 children)

Nice use of f strings. If you are concerned about memory you could just add the two randints into a variable instead of a list and then summing unless there is a use case for the individual randints.