Reputation:
function = input('Enter function')
a = input('do you want to enter another function')
b = [function]
if a.lower() == 'yes':
while True:
function1 = input('Next Function')
b += function1
if function1 == 'quit':
break
print(b)
in this code if I input in function1: y = 9x + 1; it will put the values in the array, but in the format: 'y', ' ', '9x', ' ', '+', ' ', '1'
.
how do save the input as y = 9x + 1'?
Also when I write quit, it prints the array, but the final value is q','u','i','t'
.
How do I eliminate those values?
Upvotes: 3
Views: 1528
Reputation: 370
I would recommend adding /n at the end of the string
function = input('Enter function\n')
a = input('do you want to enter another function\n')
b = [function]
if a.lower() == 'yes':
while True:
function1 = input('Next Function\n')
b.append(function1)
if function1 == 'quit':
break
print(b[:-1])
u should use append. and see here that the last element u are adding to the answer was the value the user entered "quit" you can either remove it from the list or just don't print the last item of the list as I did in my example or don't even put it in there in the first place.
Upvotes: 1
Reputation: 310
For your first request you can simply use .append()
on b
to append the element to the list b
.
Code solving first issue:
b.append(function1)
For your second request you could simply check if quit was typed before appending element to b
Code solving second issue:
while True:
function1 = input('Next Function: ')
if function1 == 'quit':
break
b.append(function1)
Final Code:
function = input('Enter function: ')
a = input('Do you want to enter another function: ')
b = [function]
if a.lower() == 'yes':
while True:
function1 = input('Next Function: ')
if function1 == 'quit':
break
b.append(function1)
print(b)
Upvotes: 2
Reputation: 473
In order to save "quit" as it is, you just need to change b += function1
with b.append(function1)
.
The corrected code is the following:
function = input('Enter function')
a = input('do you want to enter another function')
b = [function]
if a.lower() == 'yes':
while True:
function1 = input('Next Function')
b.append(function1)
if function1 == 'quit':
break
print(b)
In case you don't want the 'quit' string at all in the list, you can change the code as follows:
function = input('Enter function')
a = input('do you want to enter another function')
b = [function]
if a.lower() == 'yes':
while True:
function1 = input('Next Function')
if function1 == 'quit':
break
else:
b.append(function1)
print(b)
Additionally, this code is already saving the input y = 9x + 1
as it is (it does not save it as 'y', ' ', '9x', ' ', '+', ' ', '1'
Upvotes: 2