Before I begin- let it be known that my class IS allowed to seek outside help for this assignment, provided we do not copy code directly. What I am asking for is help, not blatantly dishonestly obtained code. I am not intending to cheat by asking this question in any way.
Now that that's cleared up....
Here's the assignment:
#1: Write a function scalar_mult(s, v) that takes a number, s, and a list, v and returns the scalar multiple of v by s.
For example:
def scalar_mult(s, v):
"""
>>> scalar_mult(5, [1, 2])
[5, 10]
>>> scalar_mult(3, [1, 0, -1])
[3, 0, -3]
>>> scalar_mult(7, [3, 0, 5, 11, 2])
[21, 0, 35, 77, 14]
"""
I have begun that part and this is what I have:
import math
s = input("Please enter a single number to be our scalar value: ")
v = input("Please enter a vector value, like [1, 2] or [5, 6, 3], to be our vector value: ")
#Note to self: TUPLES use the parentheses. LISTS use the brackets
print "scalar_mult(", s, ",", + v, "is:"
print v * s
scalar_mult(s, v)
But I keep getting this error message:
print "scalar_mult(", s, ",", + v, "is:"
TypeError: bad operand type for unary +: 'list'
Do you understand how to fix this?
And then there's a part two...
#2: Write a function replace(s, old, new) that replaces all occurrences of old with new in a string s.
For example:
def replace(s, old, new):
"""
>>> replace('Mississippi', 'i', 'I')
'MIssIssIppI'
>>> s = 'I love spom! Spom is my favorite food. Spom, spom, spom, yum!'
>>> replace(s, 'om', 'am')
'I love spam! Spam is my favorite food. Spam, spam, spam, yum!'
>>> replace(s, 'o', 'a')
'I lave spam! Spam is my favarite faad. Spam, spam, spam, yum!' """
"""
I have not yet begun #2 but I don't really understand how to approach it. Any ideas on how to begin or how it works?
This is due Friday and was assigned yesterday. Just an FYI.
THANKS SO MUCH in advance to anyone who answers -- I know this is a pretty huge question to ask >.<
If you need any clarification of the assignment please tell me! Any help would be GREATLY appreciated :)