5 Replies - 5516 Views - Last Post: 01 October 2011 - 12:22 PM Rate Topic: -----

#1 tf72289  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-September 11

how to fill list with user input?

Posted 29 September 2011 - 12:59 PM

I need to write a program that tells the user what further courses need to be taken in order to receive degree in CS. I created 3 lists for 3 different requirement fields (core, math, and stat). I asked the user to enter how many courses they have already taken. How do I prompt the user for each class taken and create list with their input? I was told if I ask the user how many courses they have taken, I will know how many times to prompt the user.

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: how to fill list with user input?

#2 Eric115  Icon User is offline

  • coderさん
  • member icon

Reputation: 63
  • View blog
  • Posts: 696
  • Joined: 19-January 09

Re: how to fill list with user input?

Posted 30 September 2011 - 04:00 AM

Hi,
First of all, could you please post what you have so far so that we can use that as a starting point?
If you want to ask the user something, (assuming you aren't using a GUI) you can use the raw_input function.
variable = raw_input("User prompt goes here")
This will ask the user to input something (you might want to ask how many courses they are taking) and then create a for loop and go through the loop x amount of times, each time asking them which subject they have taken, these could be added to a list and when the loop is finished, you could compare that list with your other lists to work out which courses they still need to take.
Hope I could help!
Was This Post Helpful? 1
  • +
  • -

#3 tf72289  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-September 11

Re: how to fill list with user input?

Posted 30 September 2011 - 09:18 AM

I cannot figure out how to construct the for loop. Here is what I have so far:

course = raw_input("How many courses in the CS program have you taken?\n:")
for x in course:
    x = raw_input("Class:")
    classes = []
    classes.append(course)



But the for loop is not cycling through x amount of times, it just prompts the user once for "Class:"
Please help I am very new to this.

MOD EDIT: Added code tags. When posting code, ESPECIALLY in the Python forum, where it's a language in which whitespace is VERY important...

USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 30 September 2011 - 12:56 PM

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6033
  • View blog
  • Posts: 23,414
  • Joined: 23-August 08

Re: how to fill list with user input?

Posted 30 September 2011 - 12:57 PM

What are you entering in response to the question you're asking?
Was This Post Helpful? 0
  • +
  • -

#5 tf72289  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-September 11

Re: how to fill list with user input?

Posted 30 September 2011 - 01:16 PM

course = raw_input("How many courses in the CS program have you taken?\n:")
    classes = []
    for i in range(course):
        classes  = raw_input("Class: ")
    classes.append(course)


here is the output:

Enter classes in the form <MAJOR-CODE> <COURSE-NUMBER>
How many courses in the CS program have you taken?
:4
Traceback (most recent call last):
  File "hw3.py", line 28, in <module>
    main()
  File "hw3.py", line 21, in main
    for i in range(course):
TypeError: range() integer end argument expected, got str.


what am i doing wrong?

This post has been edited by JackOfAllTrades: 01 October 2011 - 12:22 PM
Reason for edit:: Fixed code tags

Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6033
  • View blog
  • Posts: 23,414
  • Joined: 23-August 08

Re: how to fill list with user input?

Posted 01 October 2011 - 12:22 PM

First off, please use code tags correctly!

:code:

If that's too complicated, see the first link in my signature.

Second, the error tells you everything you need to know. range takes an integer argument, but what is returned from raw_input is a string, so you need to coerce the return value of num_courses to an integer.

num_courses = raw_input("How many courses in the CS program have you taken?\n:")
for i in range(1, int(num_courses) + 1):
   print 'Course %d:' % i

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1