Script that helps people make better choicesParse emails and create HTML markup from attachmentsImprove my script to organize a video collectionGimme some random passwordsPole (Hackerrank)Summarize a document as a key-phrase or key-wordsInterpret English as Math and Perform OperationsImplemnting a Trie Data structures problem from HackerRank using Python3Python yelp scraperSystemd service configuration helper script
Why aren't RCS openings an issue for spacecraft heat shields?
What does it mean to have a subnet mask /32?
Potential new partner angry about first collaboration - how to answer email to close up this encounter in a graceful manner
How should I face my manager if I make a mistake because a senior coworker explained something incorrectly to me?
Why did this happen to Thanos's ships at the end of "Avengers: Endgame"?
How much code would a codegolf golf if a codegolf could golf code?
How to persuade recruiters to send me the Job Description?
Can you feel passing through the sound barrier in an F-16?
Shouldn't the "credit score" prevent Americans from going deeper and deeper into personal debt?
What is the hex versus octal timeline?
Why is Boris Johnson visiting only Paris & Berlin if every member of the EU needs to agree on a withdrawal deal?
Why don't we use Cavea-B
Why doesn't the Falcon-9 first stage use three legs to land?
What is the evidence on the danger of feeding whole blueberries and grapes to infants and toddlers?
How do I find the fastest route from Heathrow to an address in London using all forms of transport?
Fancy String Replace
Have only girls been born for a long time in this village?
In what ways can a Non-paladin access Paladin spells?
If the first law of thermodynamics ensures conservation of energy, why does it allow systems to lose energy?
How would one country purchase another?
Brexit and backstop: would changes require unanimous approval by all EU countries? Does Ireland hold a veto?
Were there 486SX revisions without an FPU on the die?
Why didn’t Doctor Strange stay in the original winning timeline?
Church Booleans
Script that helps people make better choices
Parse emails and create HTML markup from attachmentsImprove my script to organize a video collectionGimme some random passwordsPole (Hackerrank)Summarize a document as a key-phrase or key-wordsInterpret English as Math and Perform OperationsImplemnting a Trie Data structures problem from HackerRank using Python3Python yelp scraperSystemd service configuration helper script
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
This is a "Help me make a choice" script where the user can input two options and then, based on the (3) reasons why they are good options, and the reasons weight the script will tell you which option you should choose.
This is my first Python script based on an idea I had and I'd like to know how I could make it better. For example, should I store the reasons (ie. option1a) in a list, dictionary or keep them in variables? Should I do the same to the weight (ie. option1aw) of each option?
The code works and does what I wanted it to do, but I don't know if I did in the best way possible.
import random
print("First, tell me what your options are.")
option1 = input("Option 1: ")
option2 = input("Ok. Now tell me the other option: ")
option1a = input("nGood. Now tell me a reason why '' is a good choice: ".format(option1))
option1b = input("Ok. Now tell me another reason why '' is a good choice: ".format(option1))
option1c = input("Ok. Now tell me another reason why '' is a good choice: ".format(option1))
option2a = input("nThats all for ''. Now tell me why '' is a good choice: ".format(option1, option2))
option2b = input("Now tell me another reason why '' is a good choice: ".format(option2))
option2c = input("Now tell me another reason why '' is a good choice: ".format(option2))
option1aw = int(input("nNow let's evaluate the options. Regarding '', "
"from 1 to 5, tell me how important is ''? ".format(option1, option1a)))
option1bw = int(input("Still regarding '', from 1 to 5, tell me how important is ''? ".format(option1, option1b)))
option1cw = int(input("How about '', from 1 to 5, tell me how important it is: ".format(option1c)))
option2aw = int(input("nNow let's evaluate the rest of the options. Regarding '',"
" from 1 to 5, tell me how important is ''? ".format(option2, option2a)))
option2bw = int(input("Still regarding '', from 1 to 5, tell me how important is ''? ".format(option2, option2b)))
option2cw = int(input("How about '', from 1 to 5, tell me how important it is: ".format(option2c)))
prompt = input("nWe'll calculate now. Click enter when you are ready to see the results. > ")
option1result = option1aw + option1bw + option1cw
option2result = option2aw + option2bw + option2cw
options = [option1, option2]
def coinflip():
print("Your best choice is:")
print(options[random.randint(0, len(options)-1)])
if option1result > option2result:
print("n'' is your best choice based on the reasons you gave me. ".format(option1))
elif option1result == option2result:
print("nHonestly, both are good options. Do you want to flip a coin? Press enter. ")
input()
coinflip()
else:
print("n'' is your best choice based on the reasons you gave me. ".format(option2))
quit()
python python-3.x
New contributor
$endgroup$
add a comment |
$begingroup$
This is a "Help me make a choice" script where the user can input two options and then, based on the (3) reasons why they are good options, and the reasons weight the script will tell you which option you should choose.
This is my first Python script based on an idea I had and I'd like to know how I could make it better. For example, should I store the reasons (ie. option1a) in a list, dictionary or keep them in variables? Should I do the same to the weight (ie. option1aw) of each option?
The code works and does what I wanted it to do, but I don't know if I did in the best way possible.
import random
print("First, tell me what your options are.")
option1 = input("Option 1: ")
option2 = input("Ok. Now tell me the other option: ")
option1a = input("nGood. Now tell me a reason why '' is a good choice: ".format(option1))
option1b = input("Ok. Now tell me another reason why '' is a good choice: ".format(option1))
option1c = input("Ok. Now tell me another reason why '' is a good choice: ".format(option1))
option2a = input("nThats all for ''. Now tell me why '' is a good choice: ".format(option1, option2))
option2b = input("Now tell me another reason why '' is a good choice: ".format(option2))
option2c = input("Now tell me another reason why '' is a good choice: ".format(option2))
option1aw = int(input("nNow let's evaluate the options. Regarding '', "
"from 1 to 5, tell me how important is ''? ".format(option1, option1a)))
option1bw = int(input("Still regarding '', from 1 to 5, tell me how important is ''? ".format(option1, option1b)))
option1cw = int(input("How about '', from 1 to 5, tell me how important it is: ".format(option1c)))
option2aw = int(input("nNow let's evaluate the rest of the options. Regarding '',"
" from 1 to 5, tell me how important is ''? ".format(option2, option2a)))
option2bw = int(input("Still regarding '', from 1 to 5, tell me how important is ''? ".format(option2, option2b)))
option2cw = int(input("How about '', from 1 to 5, tell me how important it is: ".format(option2c)))
prompt = input("nWe'll calculate now. Click enter when you are ready to see the results. > ")
option1result = option1aw + option1bw + option1cw
option2result = option2aw + option2bw + option2cw
options = [option1, option2]
def coinflip():
print("Your best choice is:")
print(options[random.randint(0, len(options)-1)])
if option1result > option2result:
print("n'' is your best choice based on the reasons you gave me. ".format(option1))
elif option1result == option2result:
print("nHonestly, both are good options. Do you want to flip a coin? Press enter. ")
input()
coinflip()
else:
print("n'' is your best choice based on the reasons you gave me. ".format(option2))
quit()
python python-3.x
New contributor
$endgroup$
add a comment |
$begingroup$
This is a "Help me make a choice" script where the user can input two options and then, based on the (3) reasons why they are good options, and the reasons weight the script will tell you which option you should choose.
This is my first Python script based on an idea I had and I'd like to know how I could make it better. For example, should I store the reasons (ie. option1a) in a list, dictionary or keep them in variables? Should I do the same to the weight (ie. option1aw) of each option?
The code works and does what I wanted it to do, but I don't know if I did in the best way possible.
import random
print("First, tell me what your options are.")
option1 = input("Option 1: ")
option2 = input("Ok. Now tell me the other option: ")
option1a = input("nGood. Now tell me a reason why '' is a good choice: ".format(option1))
option1b = input("Ok. Now tell me another reason why '' is a good choice: ".format(option1))
option1c = input("Ok. Now tell me another reason why '' is a good choice: ".format(option1))
option2a = input("nThats all for ''. Now tell me why '' is a good choice: ".format(option1, option2))
option2b = input("Now tell me another reason why '' is a good choice: ".format(option2))
option2c = input("Now tell me another reason why '' is a good choice: ".format(option2))
option1aw = int(input("nNow let's evaluate the options. Regarding '', "
"from 1 to 5, tell me how important is ''? ".format(option1, option1a)))
option1bw = int(input("Still regarding '', from 1 to 5, tell me how important is ''? ".format(option1, option1b)))
option1cw = int(input("How about '', from 1 to 5, tell me how important it is: ".format(option1c)))
option2aw = int(input("nNow let's evaluate the rest of the options. Regarding '',"
" from 1 to 5, tell me how important is ''? ".format(option2, option2a)))
option2bw = int(input("Still regarding '', from 1 to 5, tell me how important is ''? ".format(option2, option2b)))
option2cw = int(input("How about '', from 1 to 5, tell me how important it is: ".format(option2c)))
prompt = input("nWe'll calculate now. Click enter when you are ready to see the results. > ")
option1result = option1aw + option1bw + option1cw
option2result = option2aw + option2bw + option2cw
options = [option1, option2]
def coinflip():
print("Your best choice is:")
print(options[random.randint(0, len(options)-1)])
if option1result > option2result:
print("n'' is your best choice based on the reasons you gave me. ".format(option1))
elif option1result == option2result:
print("nHonestly, both are good options. Do you want to flip a coin? Press enter. ")
input()
coinflip()
else:
print("n'' is your best choice based on the reasons you gave me. ".format(option2))
quit()
python python-3.x
New contributor
$endgroup$
This is a "Help me make a choice" script where the user can input two options and then, based on the (3) reasons why they are good options, and the reasons weight the script will tell you which option you should choose.
This is my first Python script based on an idea I had and I'd like to know how I could make it better. For example, should I store the reasons (ie. option1a) in a list, dictionary or keep them in variables? Should I do the same to the weight (ie. option1aw) of each option?
The code works and does what I wanted it to do, but I don't know if I did in the best way possible.
import random
print("First, tell me what your options are.")
option1 = input("Option 1: ")
option2 = input("Ok. Now tell me the other option: ")
option1a = input("nGood. Now tell me a reason why '' is a good choice: ".format(option1))
option1b = input("Ok. Now tell me another reason why '' is a good choice: ".format(option1))
option1c = input("Ok. Now tell me another reason why '' is a good choice: ".format(option1))
option2a = input("nThats all for ''. Now tell me why '' is a good choice: ".format(option1, option2))
option2b = input("Now tell me another reason why '' is a good choice: ".format(option2))
option2c = input("Now tell me another reason why '' is a good choice: ".format(option2))
option1aw = int(input("nNow let's evaluate the options. Regarding '', "
"from 1 to 5, tell me how important is ''? ".format(option1, option1a)))
option1bw = int(input("Still regarding '', from 1 to 5, tell me how important is ''? ".format(option1, option1b)))
option1cw = int(input("How about '', from 1 to 5, tell me how important it is: ".format(option1c)))
option2aw = int(input("nNow let's evaluate the rest of the options. Regarding '',"
" from 1 to 5, tell me how important is ''? ".format(option2, option2a)))
option2bw = int(input("Still regarding '', from 1 to 5, tell me how important is ''? ".format(option2, option2b)))
option2cw = int(input("How about '', from 1 to 5, tell me how important it is: ".format(option2c)))
prompt = input("nWe'll calculate now. Click enter when you are ready to see the results. > ")
option1result = option1aw + option1bw + option1cw
option2result = option2aw + option2bw + option2cw
options = [option1, option2]
def coinflip():
print("Your best choice is:")
print(options[random.randint(0, len(options)-1)])
if option1result > option2result:
print("n'' is your best choice based on the reasons you gave me. ".format(option1))
elif option1result == option2result:
print("nHonestly, both are good options. Do you want to flip a coin? Press enter. ")
input()
coinflip()
else:
print("n'' is your best choice based on the reasons you gave me. ".format(option2))
quit()
python python-3.x
python python-3.x
New contributor
New contributor
edited 7 hours ago
Peilonrayz
29.9k4 gold badges45 silver badges119 bronze badges
29.9k4 gold badges45 silver badges119 bronze badges
New contributor
asked 8 hours ago
Leo RapiniLeo Rapini
334 bronze badges
334 bronze badges
New contributor
New contributor
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
- I think your program would be easier to use and create if you rearrange when you ask your questions. (This is mostly to show that I have consciously changed how your program works)
You are correct it would be easier to use your data if you enter it as a dictionary and some lists. I personally would use the following layout:
option_1 =
'option': option1,
'reasons': [option1a, option1b, option1c],
'weights': [option1aw, option1bw, option1cw],This allows getting the relevant by indexing the objects.
For example to get the entered option you can do:option_1['option']
To get the first reason you can do:
option_1['option'][0]
It should be noted that lists in Python, and most programming languages, are indexed starting at 0, which is why to get the first value we see the 0 above.
Reduce your workload by using functions, these allow you to define a set of instructions to run which you can then reuse by calling the function.
Take the following function to get an option:
Note: I have changed the questions in this code snippet.def get_option():
return
'option': input('Enter an option: '),
'reasons': [
input(f'Option 1: Why is this a good option? '),
input(f'Option 2: Why is this a good option? '),
input(f'Option 3: Why is this a good option? '),
],
'weights': [
int(input(f'How important is Option 1? (from 1-5) ')),
int(input(f'How important is Option 2? (from 1-5) ')),
int(input(f'How important is Option 3? (from 1-5) ')),
]
Allowing a user to enter two options is now simple. You make a list with both of them.
options = [
get_option(),
get_option(),
]Before we go any further I'd like to show you my favorite feature of Python - list comprehensions. These allow you to perform a task on a list in a single line. Take the above code snippet to generate two options, we can rewrite that using standard list generation methods exposed in Python and other languages, which would look like:
options = [] # Build an empty list
for _ in range(2): # Loop twice
options.append(get_option()) # Add an option to options on each loopHowever this pattern is rather messy and it would be more Pythonic to use a comprehension here.
options = [
get_option()
for _ in range(2)
]You should be able to notice we can also use this to simplify our
get_option
code.def get_option():
return
'option': input('Enter an option: '),
'reasons': [
input(f'Reason i+1: Why is this a good option? ')
for i in range(3)
],
'weights': [
int(input(f'How important is Reason i+1? (from 1-5) '))
for i in range(3)
]
From here we can change your
option1result
andoption2result
to:results = [sum(option['weights']) for option in options]
You can use
random.choice
to chose from a list.- It is best practice to use an
if __name__ == '__main__':
guard to prevent your code from running unless it's the main program. - Don't use
quit
, if you remove it the program will exit successfully.
import random
def get_option():
return
'option': input('Enter an option: '),
'reasons': [
input(f'Reason i+1: Why is this a good option? ')
for i in range(3)
],
'weights': [
int(input(f'How important is Reason i+1? (from 1-5) '))
for i in range(3)
]
if __name__ == '__main__':
print('First, tell me what your options are.')
options = [
get_option()
for _ in range(2)
]
prompt = input('nWe will calculate now.n')
results = [sum(option['weights']) for option in options]
if results[0] == results[1]:
input('There are multiple best options.nThe best will be determined by coinflip.n')
best_option = random.choice(options)
else:
best_option = options[0] if results[0] > results[1] else results[1]
print('Your best choice is:')
print(options[0]['option'])
Additional improvements:
Your code only currently works for two options. To get the top options for any amount of options is fairly simple.
You want a dictionary that holds all the options with a certain total weight. After this you just want to take the max weight, which will give you all the options with that weight.
Once you have have the best options the code is pretty much the same, if there are multiple options then you just use
random.choice
on them to narrow them down to one.You should allow your user to enter how many options and reasons they want. This now is just a simple question you can ask before entering either loop.
- The way you get user input is error prone, if I enter
a
as my weight then your code blows up.
You should also take into account that you've told your user that only 1-5 are valid entries, but happily allow -1, and 6. - Not displaying all the best options seems like a poor oversight. Since we know all the best options we can just display them by looping.
Ignoring 3 this can get:
import random
def get_option():
option = input('Enter an option: ')
reasons = int(input('How many reasons do you have for this option? '))
return
'option': option,
'reasons': [
input(f'Reason i+1: Why is this a good option? ')
for i in range(reasons)
],
'weights': [
int(input(f'How important is Reason i+1? (from 1-5) '))
for i in range(reasons)
]
def get_top_options(options):
rankings =
for option in options:
rankings.setdefault(sum(option['weights']), []).append(option)
return rankings[max(rankings)]
if __name__ == '__main__':
amount_options = int(input('How many options are there? '))
options = [
get_option()
for _ in range(amount_options)
]
prompt = input('nWe will calculate now.n')
best_options = get_top_options(options)
if len(best_options) == 1:
print('Your best choice is:')
else:
print('Your best choices are:')
for option in best_options:
print(option['option'])
$endgroup$
$begingroup$
Thank you for taking the time to reply so extensively. It was basically a Python lesson!! I'll study this later today.
$endgroup$
– Leo Rapini
6 hours ago
$begingroup$
@LeoRapini No problem, thank you for the reputation. I have also included a couple more improvements. Happy learning :)
$endgroup$
– Peilonrayz
4 hours ago
add a comment |
$begingroup$
Data instead of code
You have a lot of repeated calls to input. This should really just be a tuple of strings that all refer to a choices dict; something like:
choices =
prompts = (
('option1': 'Option 1:'),
# ...
('option1a': 'Good. Now tell me a reason why option1 is a good choice: '),
# ...
)
for name, prompt in prompts:
choices[name] = input(prompt.format(**choices))
Global code
Move most of your global statements into functions, with a top-level main
function.
Quit
...at the end is redundant.
The illusion of choice
Don't ask the user whether they want to flip a coin, only to do it anyway. Either given them an actual choice, or just say that it's going to happen.
$endgroup$
$begingroup$
Thank you for your reply @Reinderien. I love the Illusion of choice in the end!!
$endgroup$
– Leo Rapini
6 hours ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Leo Rapini is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f226574%2fscript-that-helps-people-make-better-choices%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
- I think your program would be easier to use and create if you rearrange when you ask your questions. (This is mostly to show that I have consciously changed how your program works)
You are correct it would be easier to use your data if you enter it as a dictionary and some lists. I personally would use the following layout:
option_1 =
'option': option1,
'reasons': [option1a, option1b, option1c],
'weights': [option1aw, option1bw, option1cw],This allows getting the relevant by indexing the objects.
For example to get the entered option you can do:option_1['option']
To get the first reason you can do:
option_1['option'][0]
It should be noted that lists in Python, and most programming languages, are indexed starting at 0, which is why to get the first value we see the 0 above.
Reduce your workload by using functions, these allow you to define a set of instructions to run which you can then reuse by calling the function.
Take the following function to get an option:
Note: I have changed the questions in this code snippet.def get_option():
return
'option': input('Enter an option: '),
'reasons': [
input(f'Option 1: Why is this a good option? '),
input(f'Option 2: Why is this a good option? '),
input(f'Option 3: Why is this a good option? '),
],
'weights': [
int(input(f'How important is Option 1? (from 1-5) ')),
int(input(f'How important is Option 2? (from 1-5) ')),
int(input(f'How important is Option 3? (from 1-5) ')),
]
Allowing a user to enter two options is now simple. You make a list with both of them.
options = [
get_option(),
get_option(),
]Before we go any further I'd like to show you my favorite feature of Python - list comprehensions. These allow you to perform a task on a list in a single line. Take the above code snippet to generate two options, we can rewrite that using standard list generation methods exposed in Python and other languages, which would look like:
options = [] # Build an empty list
for _ in range(2): # Loop twice
options.append(get_option()) # Add an option to options on each loopHowever this pattern is rather messy and it would be more Pythonic to use a comprehension here.
options = [
get_option()
for _ in range(2)
]You should be able to notice we can also use this to simplify our
get_option
code.def get_option():
return
'option': input('Enter an option: '),
'reasons': [
input(f'Reason i+1: Why is this a good option? ')
for i in range(3)
],
'weights': [
int(input(f'How important is Reason i+1? (from 1-5) '))
for i in range(3)
]
From here we can change your
option1result
andoption2result
to:results = [sum(option['weights']) for option in options]
You can use
random.choice
to chose from a list.- It is best practice to use an
if __name__ == '__main__':
guard to prevent your code from running unless it's the main program. - Don't use
quit
, if you remove it the program will exit successfully.
import random
def get_option():
return
'option': input('Enter an option: '),
'reasons': [
input(f'Reason i+1: Why is this a good option? ')
for i in range(3)
],
'weights': [
int(input(f'How important is Reason i+1? (from 1-5) '))
for i in range(3)
]
if __name__ == '__main__':
print('First, tell me what your options are.')
options = [
get_option()
for _ in range(2)
]
prompt = input('nWe will calculate now.n')
results = [sum(option['weights']) for option in options]
if results[0] == results[1]:
input('There are multiple best options.nThe best will be determined by coinflip.n')
best_option = random.choice(options)
else:
best_option = options[0] if results[0] > results[1] else results[1]
print('Your best choice is:')
print(options[0]['option'])
Additional improvements:
Your code only currently works for two options. To get the top options for any amount of options is fairly simple.
You want a dictionary that holds all the options with a certain total weight. After this you just want to take the max weight, which will give you all the options with that weight.
Once you have have the best options the code is pretty much the same, if there are multiple options then you just use
random.choice
on them to narrow them down to one.You should allow your user to enter how many options and reasons they want. This now is just a simple question you can ask before entering either loop.
- The way you get user input is error prone, if I enter
a
as my weight then your code blows up.
You should also take into account that you've told your user that only 1-5 are valid entries, but happily allow -1, and 6. - Not displaying all the best options seems like a poor oversight. Since we know all the best options we can just display them by looping.
Ignoring 3 this can get:
import random
def get_option():
option = input('Enter an option: ')
reasons = int(input('How many reasons do you have for this option? '))
return
'option': option,
'reasons': [
input(f'Reason i+1: Why is this a good option? ')
for i in range(reasons)
],
'weights': [
int(input(f'How important is Reason i+1? (from 1-5) '))
for i in range(reasons)
]
def get_top_options(options):
rankings =
for option in options:
rankings.setdefault(sum(option['weights']), []).append(option)
return rankings[max(rankings)]
if __name__ == '__main__':
amount_options = int(input('How many options are there? '))
options = [
get_option()
for _ in range(amount_options)
]
prompt = input('nWe will calculate now.n')
best_options = get_top_options(options)
if len(best_options) == 1:
print('Your best choice is:')
else:
print('Your best choices are:')
for option in best_options:
print(option['option'])
$endgroup$
$begingroup$
Thank you for taking the time to reply so extensively. It was basically a Python lesson!! I'll study this later today.
$endgroup$
– Leo Rapini
6 hours ago
$begingroup$
@LeoRapini No problem, thank you for the reputation. I have also included a couple more improvements. Happy learning :)
$endgroup$
– Peilonrayz
4 hours ago
add a comment |
$begingroup$
- I think your program would be easier to use and create if you rearrange when you ask your questions. (This is mostly to show that I have consciously changed how your program works)
You are correct it would be easier to use your data if you enter it as a dictionary and some lists. I personally would use the following layout:
option_1 =
'option': option1,
'reasons': [option1a, option1b, option1c],
'weights': [option1aw, option1bw, option1cw],This allows getting the relevant by indexing the objects.
For example to get the entered option you can do:option_1['option']
To get the first reason you can do:
option_1['option'][0]
It should be noted that lists in Python, and most programming languages, are indexed starting at 0, which is why to get the first value we see the 0 above.
Reduce your workload by using functions, these allow you to define a set of instructions to run which you can then reuse by calling the function.
Take the following function to get an option:
Note: I have changed the questions in this code snippet.def get_option():
return
'option': input('Enter an option: '),
'reasons': [
input(f'Option 1: Why is this a good option? '),
input(f'Option 2: Why is this a good option? '),
input(f'Option 3: Why is this a good option? '),
],
'weights': [
int(input(f'How important is Option 1? (from 1-5) ')),
int(input(f'How important is Option 2? (from 1-5) ')),
int(input(f'How important is Option 3? (from 1-5) ')),
]
Allowing a user to enter two options is now simple. You make a list with both of them.
options = [
get_option(),
get_option(),
]Before we go any further I'd like to show you my favorite feature of Python - list comprehensions. These allow you to perform a task on a list in a single line. Take the above code snippet to generate two options, we can rewrite that using standard list generation methods exposed in Python and other languages, which would look like:
options = [] # Build an empty list
for _ in range(2): # Loop twice
options.append(get_option()) # Add an option to options on each loopHowever this pattern is rather messy and it would be more Pythonic to use a comprehension here.
options = [
get_option()
for _ in range(2)
]You should be able to notice we can also use this to simplify our
get_option
code.def get_option():
return
'option': input('Enter an option: '),
'reasons': [
input(f'Reason i+1: Why is this a good option? ')
for i in range(3)
],
'weights': [
int(input(f'How important is Reason i+1? (from 1-5) '))
for i in range(3)
]
From here we can change your
option1result
andoption2result
to:results = [sum(option['weights']) for option in options]
You can use
random.choice
to chose from a list.- It is best practice to use an
if __name__ == '__main__':
guard to prevent your code from running unless it's the main program. - Don't use
quit
, if you remove it the program will exit successfully.
import random
def get_option():
return
'option': input('Enter an option: '),
'reasons': [
input(f'Reason i+1: Why is this a good option? ')
for i in range(3)
],
'weights': [
int(input(f'How important is Reason i+1? (from 1-5) '))
for i in range(3)
]
if __name__ == '__main__':
print('First, tell me what your options are.')
options = [
get_option()
for _ in range(2)
]
prompt = input('nWe will calculate now.n')
results = [sum(option['weights']) for option in options]
if results[0] == results[1]:
input('There are multiple best options.nThe best will be determined by coinflip.n')
best_option = random.choice(options)
else:
best_option = options[0] if results[0] > results[1] else results[1]
print('Your best choice is:')
print(options[0]['option'])
Additional improvements:
Your code only currently works for two options. To get the top options for any amount of options is fairly simple.
You want a dictionary that holds all the options with a certain total weight. After this you just want to take the max weight, which will give you all the options with that weight.
Once you have have the best options the code is pretty much the same, if there are multiple options then you just use
random.choice
on them to narrow them down to one.You should allow your user to enter how many options and reasons they want. This now is just a simple question you can ask before entering either loop.
- The way you get user input is error prone, if I enter
a
as my weight then your code blows up.
You should also take into account that you've told your user that only 1-5 are valid entries, but happily allow -1, and 6. - Not displaying all the best options seems like a poor oversight. Since we know all the best options we can just display them by looping.
Ignoring 3 this can get:
import random
def get_option():
option = input('Enter an option: ')
reasons = int(input('How many reasons do you have for this option? '))
return
'option': option,
'reasons': [
input(f'Reason i+1: Why is this a good option? ')
for i in range(reasons)
],
'weights': [
int(input(f'How important is Reason i+1? (from 1-5) '))
for i in range(reasons)
]
def get_top_options(options):
rankings =
for option in options:
rankings.setdefault(sum(option['weights']), []).append(option)
return rankings[max(rankings)]
if __name__ == '__main__':
amount_options = int(input('How many options are there? '))
options = [
get_option()
for _ in range(amount_options)
]
prompt = input('nWe will calculate now.n')
best_options = get_top_options(options)
if len(best_options) == 1:
print('Your best choice is:')
else:
print('Your best choices are:')
for option in best_options:
print(option['option'])
$endgroup$
$begingroup$
Thank you for taking the time to reply so extensively. It was basically a Python lesson!! I'll study this later today.
$endgroup$
– Leo Rapini
6 hours ago
$begingroup$
@LeoRapini No problem, thank you for the reputation. I have also included a couple more improvements. Happy learning :)
$endgroup$
– Peilonrayz
4 hours ago
add a comment |
$begingroup$
- I think your program would be easier to use and create if you rearrange when you ask your questions. (This is mostly to show that I have consciously changed how your program works)
You are correct it would be easier to use your data if you enter it as a dictionary and some lists. I personally would use the following layout:
option_1 =
'option': option1,
'reasons': [option1a, option1b, option1c],
'weights': [option1aw, option1bw, option1cw],This allows getting the relevant by indexing the objects.
For example to get the entered option you can do:option_1['option']
To get the first reason you can do:
option_1['option'][0]
It should be noted that lists in Python, and most programming languages, are indexed starting at 0, which is why to get the first value we see the 0 above.
Reduce your workload by using functions, these allow you to define a set of instructions to run which you can then reuse by calling the function.
Take the following function to get an option:
Note: I have changed the questions in this code snippet.def get_option():
return
'option': input('Enter an option: '),
'reasons': [
input(f'Option 1: Why is this a good option? '),
input(f'Option 2: Why is this a good option? '),
input(f'Option 3: Why is this a good option? '),
],
'weights': [
int(input(f'How important is Option 1? (from 1-5) ')),
int(input(f'How important is Option 2? (from 1-5) ')),
int(input(f'How important is Option 3? (from 1-5) ')),
]
Allowing a user to enter two options is now simple. You make a list with both of them.
options = [
get_option(),
get_option(),
]Before we go any further I'd like to show you my favorite feature of Python - list comprehensions. These allow you to perform a task on a list in a single line. Take the above code snippet to generate two options, we can rewrite that using standard list generation methods exposed in Python and other languages, which would look like:
options = [] # Build an empty list
for _ in range(2): # Loop twice
options.append(get_option()) # Add an option to options on each loopHowever this pattern is rather messy and it would be more Pythonic to use a comprehension here.
options = [
get_option()
for _ in range(2)
]You should be able to notice we can also use this to simplify our
get_option
code.def get_option():
return
'option': input('Enter an option: '),
'reasons': [
input(f'Reason i+1: Why is this a good option? ')
for i in range(3)
],
'weights': [
int(input(f'How important is Reason i+1? (from 1-5) '))
for i in range(3)
]
From here we can change your
option1result
andoption2result
to:results = [sum(option['weights']) for option in options]
You can use
random.choice
to chose from a list.- It is best practice to use an
if __name__ == '__main__':
guard to prevent your code from running unless it's the main program. - Don't use
quit
, if you remove it the program will exit successfully.
import random
def get_option():
return
'option': input('Enter an option: '),
'reasons': [
input(f'Reason i+1: Why is this a good option? ')
for i in range(3)
],
'weights': [
int(input(f'How important is Reason i+1? (from 1-5) '))
for i in range(3)
]
if __name__ == '__main__':
print('First, tell me what your options are.')
options = [
get_option()
for _ in range(2)
]
prompt = input('nWe will calculate now.n')
results = [sum(option['weights']) for option in options]
if results[0] == results[1]:
input('There are multiple best options.nThe best will be determined by coinflip.n')
best_option = random.choice(options)
else:
best_option = options[0] if results[0] > results[1] else results[1]
print('Your best choice is:')
print(options[0]['option'])
Additional improvements:
Your code only currently works for two options. To get the top options for any amount of options is fairly simple.
You want a dictionary that holds all the options with a certain total weight. After this you just want to take the max weight, which will give you all the options with that weight.
Once you have have the best options the code is pretty much the same, if there are multiple options then you just use
random.choice
on them to narrow them down to one.You should allow your user to enter how many options and reasons they want. This now is just a simple question you can ask before entering either loop.
- The way you get user input is error prone, if I enter
a
as my weight then your code blows up.
You should also take into account that you've told your user that only 1-5 are valid entries, but happily allow -1, and 6. - Not displaying all the best options seems like a poor oversight. Since we know all the best options we can just display them by looping.
Ignoring 3 this can get:
import random
def get_option():
option = input('Enter an option: ')
reasons = int(input('How many reasons do you have for this option? '))
return
'option': option,
'reasons': [
input(f'Reason i+1: Why is this a good option? ')
for i in range(reasons)
],
'weights': [
int(input(f'How important is Reason i+1? (from 1-5) '))
for i in range(reasons)
]
def get_top_options(options):
rankings =
for option in options:
rankings.setdefault(sum(option['weights']), []).append(option)
return rankings[max(rankings)]
if __name__ == '__main__':
amount_options = int(input('How many options are there? '))
options = [
get_option()
for _ in range(amount_options)
]
prompt = input('nWe will calculate now.n')
best_options = get_top_options(options)
if len(best_options) == 1:
print('Your best choice is:')
else:
print('Your best choices are:')
for option in best_options:
print(option['option'])
$endgroup$
- I think your program would be easier to use and create if you rearrange when you ask your questions. (This is mostly to show that I have consciously changed how your program works)
You are correct it would be easier to use your data if you enter it as a dictionary and some lists. I personally would use the following layout:
option_1 =
'option': option1,
'reasons': [option1a, option1b, option1c],
'weights': [option1aw, option1bw, option1cw],This allows getting the relevant by indexing the objects.
For example to get the entered option you can do:option_1['option']
To get the first reason you can do:
option_1['option'][0]
It should be noted that lists in Python, and most programming languages, are indexed starting at 0, which is why to get the first value we see the 0 above.
Reduce your workload by using functions, these allow you to define a set of instructions to run which you can then reuse by calling the function.
Take the following function to get an option:
Note: I have changed the questions in this code snippet.def get_option():
return
'option': input('Enter an option: '),
'reasons': [
input(f'Option 1: Why is this a good option? '),
input(f'Option 2: Why is this a good option? '),
input(f'Option 3: Why is this a good option? '),
],
'weights': [
int(input(f'How important is Option 1? (from 1-5) ')),
int(input(f'How important is Option 2? (from 1-5) ')),
int(input(f'How important is Option 3? (from 1-5) ')),
]
Allowing a user to enter two options is now simple. You make a list with both of them.
options = [
get_option(),
get_option(),
]Before we go any further I'd like to show you my favorite feature of Python - list comprehensions. These allow you to perform a task on a list in a single line. Take the above code snippet to generate two options, we can rewrite that using standard list generation methods exposed in Python and other languages, which would look like:
options = [] # Build an empty list
for _ in range(2): # Loop twice
options.append(get_option()) # Add an option to options on each loopHowever this pattern is rather messy and it would be more Pythonic to use a comprehension here.
options = [
get_option()
for _ in range(2)
]You should be able to notice we can also use this to simplify our
get_option
code.def get_option():
return
'option': input('Enter an option: '),
'reasons': [
input(f'Reason i+1: Why is this a good option? ')
for i in range(3)
],
'weights': [
int(input(f'How important is Reason i+1? (from 1-5) '))
for i in range(3)
]
From here we can change your
option1result
andoption2result
to:results = [sum(option['weights']) for option in options]
You can use
random.choice
to chose from a list.- It is best practice to use an
if __name__ == '__main__':
guard to prevent your code from running unless it's the main program. - Don't use
quit
, if you remove it the program will exit successfully.
import random
def get_option():
return
'option': input('Enter an option: '),
'reasons': [
input(f'Reason i+1: Why is this a good option? ')
for i in range(3)
],
'weights': [
int(input(f'How important is Reason i+1? (from 1-5) '))
for i in range(3)
]
if __name__ == '__main__':
print('First, tell me what your options are.')
options = [
get_option()
for _ in range(2)
]
prompt = input('nWe will calculate now.n')
results = [sum(option['weights']) for option in options]
if results[0] == results[1]:
input('There are multiple best options.nThe best will be determined by coinflip.n')
best_option = random.choice(options)
else:
best_option = options[0] if results[0] > results[1] else results[1]
print('Your best choice is:')
print(options[0]['option'])
Additional improvements:
Your code only currently works for two options. To get the top options for any amount of options is fairly simple.
You want a dictionary that holds all the options with a certain total weight. After this you just want to take the max weight, which will give you all the options with that weight.
Once you have have the best options the code is pretty much the same, if there are multiple options then you just use
random.choice
on them to narrow them down to one.You should allow your user to enter how many options and reasons they want. This now is just a simple question you can ask before entering either loop.
- The way you get user input is error prone, if I enter
a
as my weight then your code blows up.
You should also take into account that you've told your user that only 1-5 are valid entries, but happily allow -1, and 6. - Not displaying all the best options seems like a poor oversight. Since we know all the best options we can just display them by looping.
Ignoring 3 this can get:
import random
def get_option():
option = input('Enter an option: ')
reasons = int(input('How many reasons do you have for this option? '))
return
'option': option,
'reasons': [
input(f'Reason i+1: Why is this a good option? ')
for i in range(reasons)
],
'weights': [
int(input(f'How important is Reason i+1? (from 1-5) '))
for i in range(reasons)
]
def get_top_options(options):
rankings =
for option in options:
rankings.setdefault(sum(option['weights']), []).append(option)
return rankings[max(rankings)]
if __name__ == '__main__':
amount_options = int(input('How many options are there? '))
options = [
get_option()
for _ in range(amount_options)
]
prompt = input('nWe will calculate now.n')
best_options = get_top_options(options)
if len(best_options) == 1:
print('Your best choice is:')
else:
print('Your best choices are:')
for option in best_options:
print(option['option'])
edited 4 hours ago
answered 6 hours ago
PeilonrayzPeilonrayz
29.9k4 gold badges45 silver badges119 bronze badges
29.9k4 gold badges45 silver badges119 bronze badges
$begingroup$
Thank you for taking the time to reply so extensively. It was basically a Python lesson!! I'll study this later today.
$endgroup$
– Leo Rapini
6 hours ago
$begingroup$
@LeoRapini No problem, thank you for the reputation. I have also included a couple more improvements. Happy learning :)
$endgroup$
– Peilonrayz
4 hours ago
add a comment |
$begingroup$
Thank you for taking the time to reply so extensively. It was basically a Python lesson!! I'll study this later today.
$endgroup$
– Leo Rapini
6 hours ago
$begingroup$
@LeoRapini No problem, thank you for the reputation. I have also included a couple more improvements. Happy learning :)
$endgroup$
– Peilonrayz
4 hours ago
$begingroup$
Thank you for taking the time to reply so extensively. It was basically a Python lesson!! I'll study this later today.
$endgroup$
– Leo Rapini
6 hours ago
$begingroup$
Thank you for taking the time to reply so extensively. It was basically a Python lesson!! I'll study this later today.
$endgroup$
– Leo Rapini
6 hours ago
$begingroup$
@LeoRapini No problem, thank you for the reputation. I have also included a couple more improvements. Happy learning :)
$endgroup$
– Peilonrayz
4 hours ago
$begingroup$
@LeoRapini No problem, thank you for the reputation. I have also included a couple more improvements. Happy learning :)
$endgroup$
– Peilonrayz
4 hours ago
add a comment |
$begingroup$
Data instead of code
You have a lot of repeated calls to input. This should really just be a tuple of strings that all refer to a choices dict; something like:
choices =
prompts = (
('option1': 'Option 1:'),
# ...
('option1a': 'Good. Now tell me a reason why option1 is a good choice: '),
# ...
)
for name, prompt in prompts:
choices[name] = input(prompt.format(**choices))
Global code
Move most of your global statements into functions, with a top-level main
function.
Quit
...at the end is redundant.
The illusion of choice
Don't ask the user whether they want to flip a coin, only to do it anyway. Either given them an actual choice, or just say that it's going to happen.
$endgroup$
$begingroup$
Thank you for your reply @Reinderien. I love the Illusion of choice in the end!!
$endgroup$
– Leo Rapini
6 hours ago
add a comment |
$begingroup$
Data instead of code
You have a lot of repeated calls to input. This should really just be a tuple of strings that all refer to a choices dict; something like:
choices =
prompts = (
('option1': 'Option 1:'),
# ...
('option1a': 'Good. Now tell me a reason why option1 is a good choice: '),
# ...
)
for name, prompt in prompts:
choices[name] = input(prompt.format(**choices))
Global code
Move most of your global statements into functions, with a top-level main
function.
Quit
...at the end is redundant.
The illusion of choice
Don't ask the user whether they want to flip a coin, only to do it anyway. Either given them an actual choice, or just say that it's going to happen.
$endgroup$
$begingroup$
Thank you for your reply @Reinderien. I love the Illusion of choice in the end!!
$endgroup$
– Leo Rapini
6 hours ago
add a comment |
$begingroup$
Data instead of code
You have a lot of repeated calls to input. This should really just be a tuple of strings that all refer to a choices dict; something like:
choices =
prompts = (
('option1': 'Option 1:'),
# ...
('option1a': 'Good. Now tell me a reason why option1 is a good choice: '),
# ...
)
for name, prompt in prompts:
choices[name] = input(prompt.format(**choices))
Global code
Move most of your global statements into functions, with a top-level main
function.
Quit
...at the end is redundant.
The illusion of choice
Don't ask the user whether they want to flip a coin, only to do it anyway. Either given them an actual choice, or just say that it's going to happen.
$endgroup$
Data instead of code
You have a lot of repeated calls to input. This should really just be a tuple of strings that all refer to a choices dict; something like:
choices =
prompts = (
('option1': 'Option 1:'),
# ...
('option1a': 'Good. Now tell me a reason why option1 is a good choice: '),
# ...
)
for name, prompt in prompts:
choices[name] = input(prompt.format(**choices))
Global code
Move most of your global statements into functions, with a top-level main
function.
Quit
...at the end is redundant.
The illusion of choice
Don't ask the user whether they want to flip a coin, only to do it anyway. Either given them an actual choice, or just say that it's going to happen.
answered 7 hours ago
ReinderienReinderien
6,47110 silver badges29 bronze badges
6,47110 silver badges29 bronze badges
$begingroup$
Thank you for your reply @Reinderien. I love the Illusion of choice in the end!!
$endgroup$
– Leo Rapini
6 hours ago
add a comment |
$begingroup$
Thank you for your reply @Reinderien. I love the Illusion of choice in the end!!
$endgroup$
– Leo Rapini
6 hours ago
$begingroup$
Thank you for your reply @Reinderien. I love the Illusion of choice in the end!!
$endgroup$
– Leo Rapini
6 hours ago
$begingroup$
Thank you for your reply @Reinderien. I love the Illusion of choice in the end!!
$endgroup$
– Leo Rapini
6 hours ago
add a comment |
Leo Rapini is a new contributor. Be nice, and check out our Code of Conduct.
Leo Rapini is a new contributor. Be nice, and check out our Code of Conduct.
Leo Rapini is a new contributor. Be nice, and check out our Code of Conduct.
Leo Rapini is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f226574%2fscript-that-helps-people-make-better-choices%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown