I am doing a python project, its a review maker kind using a sample string and replacing the symbol in the same line of the multistring then printing it with (check mark) next to the same text in the string as user input , I have a problem in replacing a certain symbol (☑) with the same line of string which equal to user input. Here is a code example to get the idea:
JavaScript
x
33
33
1
import string
2
import re
3
4
#this is a review maker with check boxes
5
Graphics = """
6
☐ You forget what reality is,
7
☐ Beautiful,
8
☐ Good,
9
☐ Decent,
10
☐ Bad,
11
☐ Don't look too long at it,
12
☐ MS-DOS
13
"""
14
def findAndReplace(Graphics):
15
find_symbol = re.search(symbol = "☐")
16
new_symbol =re.replace(symbol1 = "☑")
17
return new_symbol
18
19
# ask question to the user and replace symbol to same answer of the user.
20
21
print = "Choose an answer each question to write the review"
22
# the strings down is equal to the Graphics strings
23
print = " Beautiful - Good - Decent - Bad"
24
25
#control flow
26
27
user_input1 = input("what do you think about Graphics? ")
28
if user_input1 in Graphics:
29
findAndReplace(Graphics)
30
print(Graphics)
31
else:
32
print("we still have errors -")
33
Advertisement
Answer
You might be looking for something on the lines of :
JavaScript
1
39
39
1
# -*- coding: utf-8 -*-
2
3
4
import string
5
import re
6
7
#this is a review maker with check boxes
8
Graphics = """
9
☐ You forget what reality is,
10
☐ Beautiful,
11
☐ Good,
12
☐ Decent,
13
☐ Bad,
14
☐ Don't look too long at it,
15
☐ MS-DOS
16
"""
17
def findAndReplace(line, line_num):
18
replacement_string = re.sub("☐", "☑", line)
19
mylist[line_num] = replacement_string
20
return "n".join(mylist)
21
22
# ask question to the user and replace symbol to same answer of the user.
23
24
print( "Choose an answer each question to write the review")
25
# the strings down is equal to the Graphics strings
26
print(" Beautiful - Good - Decent - Bad")
27
28
#control flow
29
30
mylist = Graphics.split('n')
31
user_input1 = input("what do you think about Graphics? ")
32
for i, line in enumerate(mylist):
33
try:
34
if user_input1 in line:
35
Graphics = findAndReplace(line, i)
36
print(Graphics)
37
except:
38
print("we still have errors -")
39
Another perhaps better method leveraging regular expressions is:
JavaScript
1
45
45
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
5
import string
6
import re
7
8
#this is a review maker with check boxes
9
Graphics = """
10
☐ You forget what reality is,
11
☐ Beautiful,
12
☐ Good,
13
☐ Decent,
14
☐ Bad,
15
☐ Don't look too long at it,
16
☐ MS-DOS
17
"""
18
19
# ask question to the user and replace symbol to same answer of the user.
20
21
print( "Choose an answer each question to write the review")
22
# the strings down is equal to the Graphics strings
23
print(" Beautiful - Good - Decent - Bad")
24
25
26
#control flow
27
28
user_input1 = input("what do you think about Graphics? ")
29
30
try:
31
result = re.search(user_input1, Graphics)
32
33
first = Graphics[:result.start()]
34
last = Graphics[result.start():]
35
36
start = first.rfind("☐")
37
38
new_string = first[:start] + first[start:].replace("☐", "☑")
39
40
Graphics = new_string + last
41
42
print(Graphics)
43
except:
44
print("we still have errors -")
45