1

I am new to python and just need a tad of help with this test program I am using to revise my learning.

I am getting an error on the last question of this program:

var1 = [["Carl", 1], ["Josh", 3]]
input("Please choose var1[0] or var1[1]")
if input == var1[0]:
    input("would you like to print the string or the int?(0 = str, 1 = int)")
    if input == 0:
        print(var1[0[0]])
    else:
        print(var1[0[1]])

else:
    input("would you like to print the string or the int?(0 = str, 1 = int)")
    if input == 0:
        print(var1[1[0]])
    else:
        print(var1[1[1]])
1
  • 1
    You're looking for var[0][0] not var[0[0]] Commented Nov 11, 2013 at 22:23

1 Answer 1

4

You have three problems:

  1. You are indexing your array wrong. The syntax should be like this: print(var1[0][0])
  2. You need to compare the input with strings, since input returns a string object.
  3. You need to assign that input to a variable so that you can use it later. Right now, you are comparing with the built-in input itself.

Here is your code with those problems fixed:

var1 = [["Carl", 1], ["Josh", 3]]
user_input = input("Please choose var1[0] or var1[1]")
if user_input == var1[0]:
    user_input = input("would you like to print the string or the int?(0 = str, 1 = int)")
    if user_input == '0':
        print(var1[0][0])
    else:
        print(var1[0][1])

else:
    user_input = input("would you like to print the string or the int?(0 = str, 1 = int)")
    if user_input == '0':
        print(var1[1][0])
    else:
        print(var1[1][1])
Sign up to request clarification or add additional context in comments.

3 Comments

That's what fast problem solving is ;-).
The result of input("whatever") doesn't seem to be assigned anywhere, and the comparisons are currently with the function input.
@user2980960 - Happy to help! Don't forget to accept my answer (click the tick) so people know this question is answered.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.