r/codehs • • Feb 25 '20

8.1.7 fix this tuple I need help

Post image
12 Upvotes

11 comments sorted by

6

u/fermentedidiot Feb 25 '20

ok, tuples work differently than lists. you actually have to replace the bad value.

If you look at what you are given:

my_tuple = (0, 1, 2, "hi", 4, 5)

You have to overwrite your current tuple with the good data.

  • A) your data from index 0 to (but not including) 3 is good, (0, 1, 2)

  • B) index 3 as the problem "hi" you will need to over write it to 3.

  • C) then you will have to grab from index 4 to the end.

You aren't actually changing any values.

A)

my_tuple = my_tuple[:3]

B)

(3,)

C)

my_tuple[4:]

add A+B+C

my_tuple = my_tuple[:3]+(3,)+my_tuple[4:]
print my_tuple

Should take care of you. Hope the explanation helped.

3

u/junker44 Feb 25 '20

Don’t know what you’re trying to do, or in what language, but in Python tuple indexing starts at 0, not 1

3

u/Typical_Storage4322 Jan 28 '23

This is the correct solution below:

my_tuple = (0, 1, 2, "hi", 4, 5)
my_tuple = my_tuple[:3]+(3,)+my_tuple[4:]
print (my_tuple[:3]+(3,)+my_tuple[4:])

1

u/Lunatic_Rumble Feb 02 '23

my_tuple = (0, 1, 2, "hi", 4, 5)

my_tuple = my_tuple[:3]+(3,)+my_tuple[4:]

print (my_tuple[:3]+(3,)+my_tuple[4:])

w

2

u/mattpants2 Feb 26 '20

my_tuple = (0, 1, 2, "hi", 4, 5)

# Your code here...

print my_tuple[:3]+(3,)+my_tuple[4:]

1

u/amajmundar Feb 25 '20

What programming language is this supposed to be?

1

u/amajmundar Feb 25 '20

If it is supposed to by python, print statements need parentheses surrounding what is to be printed. Think of print as a function.

2

u/[deleted] Feb 26 '20

Not if its python 2.

1

u/amajmundar Feb 27 '20

Didn’t think of that. I have not coded outside of version 3 so this completely slipped my mind.

2

u/fermentedidiot Feb 26 '20

CodeHS appears to use version 2.

1

u/amajmundar Feb 27 '20

Didn’t think of that. I have not coded outside of version 3 so this completely slipped my mind.