0
#write a program that prints out all the elements of 
the list
# are less than 5
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 
  89,6,5,3,2,1,7,9]
for x in a:
   if x < 5:
      print(x)

#Ask the user for a string and print out whether this 
 string is a
# palindrome or not.

Print("Input a text!")
txt= input()
if txt == txt[::-1]:
  print("This is a palindrome")
else:
print("This is not a Palindrome")

What would I put so that the previous code doesn't affect the future code for these python exercises? am new to python :(

yisel
  • 9
  • 2
  • 1
    Just be careful not to use previously assigned variables, or re-assign when needed? Or you can group them into several functions like `def exercise1():`, and then call them in a main chunk, like in `if __name__ == "__main__":` clause. – j1-lee Jan 19 '22 at 19:26
  • 1
    You can put each exercise into a [function definition](https://stackoverflow.com/questions/32409802/basic-explanation-of-python-functions/32409907) and only call the one of them you want to run. To have them not affect each other, make sure not to use any global variables. – lucidbrot Jan 19 '22 at 19:26

2 Answers2

0

There are various ways to handle this. First, as mentioned in previous answers, you could use different variable names to isolate ( which would not be a good practice ). Second, create functions for each separate code fragment(I strongly suggest this) so you could make a respective function call when needed.

Paul
  • 35
  • 5
0

you could use functions to isolate the code, in such way you use local variables and not globals, because using global variables will influence other code, witch would be very difficult and counterproductive.

XxJames07-
  • 1,833
  • 1
  • 4
  • 17