comparing user input to a dictionary in python
I have to create a program that lets a user input what courses they have taken(one at a time), and compare it to a dictionary of "courses" with the pre-requisites and print what courses that student is eligible to take. I am not sure on how to compare the user input to the dictionary to print what courses they can take. Here is what I have so far
print "Enter a course(0 to quit): "
courses = raw_input()
d = {150:[150],
161:[161],
162:[161],
231:[162],
241:[161],
251:[251],
260:[150],
300:[241],
303:[162],
304:[162],
307:[162],
353:[231],
385:[353],
355:[231],
461:[303,304,307,231],
475:[303,304,307],
480:[470]
}
while courses =! '':
if courses in d.keys():
print("You have taken: ", courses)
if courses == 0:
break
python
add a comment |
I have to create a program that lets a user input what courses they have taken(one at a time), and compare it to a dictionary of "courses" with the pre-requisites and print what courses that student is eligible to take. I am not sure on how to compare the user input to the dictionary to print what courses they can take. Here is what I have so far
print "Enter a course(0 to quit): "
courses = raw_input()
d = {150:[150],
161:[161],
162:[161],
231:[162],
241:[161],
251:[251],
260:[150],
300:[241],
303:[162],
304:[162],
307:[162],
353:[231],
385:[353],
355:[231],
461:[303,304,307,231],
475:[303,304,307],
480:[470]
}
while courses =! '':
if courses in d.keys():
print("You have taken: ", courses)
if courses == 0:
break
python
Be really careful when posting Python code in questions. The indentation is critical. I edited your question and put what I think is ok, but double check.
– Nic3500
Nov 26 '18 at 23:53
4
You didn't really make a question, you gave what looks like an assignment description and a small piece of code. I recommend you read SO's help center. It's ok to ask about homework, but its better to do it by giving a description of what you're having problems with.
– Aurora Wang
Nov 27 '18 at 0:05
1
@NicWerner You should not modify edits if it is not for a better version. 2 people used their time to help you editing your question and you change it again to a non working version of the code. Please read the edits and understand them. And then, edit your code again so it is actually runable.
– Netwave
Nov 27 '18 at 0:21
add a comment |
I have to create a program that lets a user input what courses they have taken(one at a time), and compare it to a dictionary of "courses" with the pre-requisites and print what courses that student is eligible to take. I am not sure on how to compare the user input to the dictionary to print what courses they can take. Here is what I have so far
print "Enter a course(0 to quit): "
courses = raw_input()
d = {150:[150],
161:[161],
162:[161],
231:[162],
241:[161],
251:[251],
260:[150],
300:[241],
303:[162],
304:[162],
307:[162],
353:[231],
385:[353],
355:[231],
461:[303,304,307,231],
475:[303,304,307],
480:[470]
}
while courses =! '':
if courses in d.keys():
print("You have taken: ", courses)
if courses == 0:
break
python
I have to create a program that lets a user input what courses they have taken(one at a time), and compare it to a dictionary of "courses" with the pre-requisites and print what courses that student is eligible to take. I am not sure on how to compare the user input to the dictionary to print what courses they can take. Here is what I have so far
print "Enter a course(0 to quit): "
courses = raw_input()
d = {150:[150],
161:[161],
162:[161],
231:[162],
241:[161],
251:[251],
260:[150],
300:[241],
303:[162],
304:[162],
307:[162],
353:[231],
385:[353],
355:[231],
461:[303,304,307,231],
475:[303,304,307],
480:[470]
}
while courses =! '':
if courses in d.keys():
print("You have taken: ", courses)
if courses == 0:
break
python
python
edited Nov 27 '18 at 0:13
Nic Werner
asked Nov 26 '18 at 23:49
Nic WernerNic Werner
72
72
Be really careful when posting Python code in questions. The indentation is critical. I edited your question and put what I think is ok, but double check.
– Nic3500
Nov 26 '18 at 23:53
4
You didn't really make a question, you gave what looks like an assignment description and a small piece of code. I recommend you read SO's help center. It's ok to ask about homework, but its better to do it by giving a description of what you're having problems with.
– Aurora Wang
Nov 27 '18 at 0:05
1
@NicWerner You should not modify edits if it is not for a better version. 2 people used their time to help you editing your question and you change it again to a non working version of the code. Please read the edits and understand them. And then, edit your code again so it is actually runable.
– Netwave
Nov 27 '18 at 0:21
add a comment |
Be really careful when posting Python code in questions. The indentation is critical. I edited your question and put what I think is ok, but double check.
– Nic3500
Nov 26 '18 at 23:53
4
You didn't really make a question, you gave what looks like an assignment description and a small piece of code. I recommend you read SO's help center. It's ok to ask about homework, but its better to do it by giving a description of what you're having problems with.
– Aurora Wang
Nov 27 '18 at 0:05
1
@NicWerner You should not modify edits if it is not for a better version. 2 people used their time to help you editing your question and you change it again to a non working version of the code. Please read the edits and understand them. And then, edit your code again so it is actually runable.
– Netwave
Nov 27 '18 at 0:21
Be really careful when posting Python code in questions. The indentation is critical. I edited your question and put what I think is ok, but double check.
– Nic3500
Nov 26 '18 at 23:53
Be really careful when posting Python code in questions. The indentation is critical. I edited your question and put what I think is ok, but double check.
– Nic3500
Nov 26 '18 at 23:53
4
4
You didn't really make a question, you gave what looks like an assignment description and a small piece of code. I recommend you read SO's help center. It's ok to ask about homework, but its better to do it by giving a description of what you're having problems with.
– Aurora Wang
Nov 27 '18 at 0:05
You didn't really make a question, you gave what looks like an assignment description and a small piece of code. I recommend you read SO's help center. It's ok to ask about homework, but its better to do it by giving a description of what you're having problems with.
– Aurora Wang
Nov 27 '18 at 0:05
1
1
@NicWerner You should not modify edits if it is not for a better version. 2 people used their time to help you editing your question and you change it again to a non working version of the code. Please read the edits and understand them. And then, edit your code again so it is actually runable.
– Netwave
Nov 27 '18 at 0:21
@NicWerner You should not modify edits if it is not for a better version. 2 people used their time to help you editing your question and you change it again to a non working version of the code. Please read the edits and understand them. And then, edit your code again so it is actually runable.
– Netwave
Nov 27 '18 at 0:21
add a comment |
1 Answer
1
active
oldest
votes
You are only getting input once. You need to get input in a loop:
d = {150:[150],161:[161],162:[161],231:[162],241:[161],251:[251],260:[150],300:[241],303:[162],304:[162],307:[162],353:[231],385:[353],355:[231],461:[303,304,307,231],475:[303,304,307],480:[470]}
prereqs = set()
while True:
course = int(raw_input("Enter a course you have taken (0 to quit): "))
if course == 0:
break
try:
prereqs.update(d[course])
except KeyError:
print 'tttttHmm...I don't know that course'
In the while loop, we are getting input every iteration. If it is 0, we break out of the loop. If not, we try to lookup the course in the dict. If this fails, we print the "error" message. You should be able to take it from here(prereqs stores the courses that you have took in a set).
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: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
});
}
});
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%2fstackoverflow.com%2fquestions%2f53490785%2fcomparing-user-input-to-a-dictionary-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You are only getting input once. You need to get input in a loop:
d = {150:[150],161:[161],162:[161],231:[162],241:[161],251:[251],260:[150],300:[241],303:[162],304:[162],307:[162],353:[231],385:[353],355:[231],461:[303,304,307,231],475:[303,304,307],480:[470]}
prereqs = set()
while True:
course = int(raw_input("Enter a course you have taken (0 to quit): "))
if course == 0:
break
try:
prereqs.update(d[course])
except KeyError:
print 'tttttHmm...I don't know that course'
In the while loop, we are getting input every iteration. If it is 0, we break out of the loop. If not, we try to lookup the course in the dict. If this fails, we print the "error" message. You should be able to take it from here(prereqs stores the courses that you have took in a set).
add a comment |
You are only getting input once. You need to get input in a loop:
d = {150:[150],161:[161],162:[161],231:[162],241:[161],251:[251],260:[150],300:[241],303:[162],304:[162],307:[162],353:[231],385:[353],355:[231],461:[303,304,307,231],475:[303,304,307],480:[470]}
prereqs = set()
while True:
course = int(raw_input("Enter a course you have taken (0 to quit): "))
if course == 0:
break
try:
prereqs.update(d[course])
except KeyError:
print 'tttttHmm...I don't know that course'
In the while loop, we are getting input every iteration. If it is 0, we break out of the loop. If not, we try to lookup the course in the dict. If this fails, we print the "error" message. You should be able to take it from here(prereqs stores the courses that you have took in a set).
add a comment |
You are only getting input once. You need to get input in a loop:
d = {150:[150],161:[161],162:[161],231:[162],241:[161],251:[251],260:[150],300:[241],303:[162],304:[162],307:[162],353:[231],385:[353],355:[231],461:[303,304,307,231],475:[303,304,307],480:[470]}
prereqs = set()
while True:
course = int(raw_input("Enter a course you have taken (0 to quit): "))
if course == 0:
break
try:
prereqs.update(d[course])
except KeyError:
print 'tttttHmm...I don't know that course'
In the while loop, we are getting input every iteration. If it is 0, we break out of the loop. If not, we try to lookup the course in the dict. If this fails, we print the "error" message. You should be able to take it from here(prereqs stores the courses that you have took in a set).
You are only getting input once. You need to get input in a loop:
d = {150:[150],161:[161],162:[161],231:[162],241:[161],251:[251],260:[150],300:[241],303:[162],304:[162],307:[162],353:[231],385:[353],355:[231],461:[303,304,307,231],475:[303,304,307],480:[470]}
prereqs = set()
while True:
course = int(raw_input("Enter a course you have taken (0 to quit): "))
if course == 0:
break
try:
prereqs.update(d[course])
except KeyError:
print 'tttttHmm...I don't know that course'
In the while loop, we are getting input every iteration. If it is 0, we break out of the loop. If not, we try to lookup the course in the dict. If this fails, we print the "error" message. You should be able to take it from here(prereqs stores the courses that you have took in a set).
answered Nov 27 '18 at 0:21
Tomothy32Tomothy32
6,9381626
6,9381626
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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.
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%2fstackoverflow.com%2fquestions%2f53490785%2fcomparing-user-input-to-a-dictionary-in-python%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
Be really careful when posting Python code in questions. The indentation is critical. I edited your question and put what I think is ok, but double check.
– Nic3500
Nov 26 '18 at 23:53
4
You didn't really make a question, you gave what looks like an assignment description and a small piece of code. I recommend you read SO's help center. It's ok to ask about homework, but its better to do it by giving a description of what you're having problems with.
– Aurora Wang
Nov 27 '18 at 0:05
1
@NicWerner You should not modify edits if it is not for a better version. 2 people used their time to help you editing your question and you change it again to a non working version of the code. Please read the edits and understand them. And then, edit your code again so it is actually runable.
– Netwave
Nov 27 '18 at 0:21