Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

Python

  • 10-05-2015 05:32PM
    #1
    Closed Accounts Posts: 431 ✭✭


    How do i get the following code to work? Basically i'm trying to take list (array) values and save them as an int, for example i want element [0] to save it's raw_input value into a int.

    Here the code snippet:
    a = 0
    
    myList=[1,2,3,4,5,6]
    a = raw_input()
    a = mylist[0]
    print (a)
    

    At the moment it gives me errors. Any clue ? only started learning python yesterday.


Comments

  • Closed Accounts Posts: 431 ✭✭whats newxt


    It print the list length but not the value.


  • Moderators, Computer Games Moderators Posts: 4,282 Mod ✭✭✭✭deconduo


    How do i get the following code to work? Basically i'm trying to take list (array) values and save them as an int, for example i want element [0] to save it's raw_input value into a int.

    Here the code snippet:
    a = 0
    
    myList=[1,2,3,4,5,6]
    a = raw_input()
    a = mylist[0]
    print (a)
    

    At the moment it gives me errors. Any clue ? only started learning python yesterday.

    Currently this is what your code does:
    a = 0
    # Sets 'a' to 0
    
    myList=[1,2,3,4,5,6]
    # Creates an array myList that contains the numbers 1 to 6
    
    a = raw_input()
    # Sets 'a' to the input from the console 
    
    a = mylist[0]
    
    # Sets 'a' to the first element in mylist.
    # This will fail as the array you defined earlier is myList not mylist.
    
    print (a)
    # Prints 'a'
    

    Apart from missing the capital L in myList, you change the value of 'a' 3 times. So the first 2 times you set it do nothing, as it gets over-written each time.

    What exactly do you expect the raw_input() function to do?


  • Closed Accounts Posts: 431 ✭✭whats newxt


    deconduo wrote: »
    Currently this is what your code does:
    a = 0
    # Sets 'a' to 0
    
    myList=[1,2,3,4,5,6]
    # Creates an array myList that contains the numbers 1 to 6
    
    a = raw_input()
    # Sets 'a' to the input from the console 
    
    a = mylist[0]
    
    # Sets 'a' to the first element in mylist.
    # This will fail as the array you defined earlier is myList not mylist.
    
    print (a)
    # Prints 'a'
    

    Apart from missing the capital L in myList, you change the value of 'a' 3 times. So the first 2 times you set it do nothing, as it gets over-written each time.

    What exactly do you expect the raw_input() function to do?

    Take user input and save the value in mylist[0] then take mylist[0] and save it value as an int and call the variable b or something. Then print b to the console.


  • Closed Accounts Posts: 431 ✭✭whats newxt


    I got it to print out the array index but not the value it holds?


  • Closed Accounts Posts: 431 ✭✭whats newxt


    It's ok i got the whole thing to work:
    a = 10#declares a
    b = 5#decalres b
    i = 0
    z = 0
    y = 0
    
    
    
    if a < b:#if argument true
        print "True"#then do this
    else:#otherwise
        print "false"#do this
    
    input = raw_input()#takes user input
    print "Your name is: " + input#prints to console + input
    
    for x in range(0, 3):#for loop X4
        i += 1#increment i by one each loop
    
    print "----------"#print to console
    print (i)#print value of i
    
    z = i*b#basic math
    print (z)#print to console
    
    mylist=[1,2,3,4,5,6]
    y = mylist[0]
    y = raw_input()
    print "_________"
    print (y)
    print "_________"
    


  • Advertisement
  • Closed Accounts Posts: 431 ✭✭whats newxt


    Python is amazing.


  • Moderators, Computer Games Moderators Posts: 4,282 Mod ✭✭✭✭deconduo


    Take user input and save the value in mylist[0] then take mylist[0] and save it value as an int and call the variable b or something.

    That's a bit of a strange way of doing things. Is there a particular reason you are doing it that way?

    If that's what you want:
    myList = [1,2,3,4,5,6]
    # Creates a list with the numbers 1 to 6
    
    a = raw_input('Enter your value: ')
    # Sets a equal to the console value
    
    myList[0] = a
    # Replaces the first element in myList with a
    
    b = int(myList[0])
    # Sets b equal to the int value of the first element in myList.
    

    So for example, if you give an input of 9, you will have:

    a = '9'
    myList =
    b = 9

    This will fail if the input given can't be parsed to an int.

    EDIT: Ah I see you figured it out yourself.


Advertisement