0

Why this code returns an unexpected "None" ?

Code: vvod = input() x = vvod.split(" ") def sum(x): print(int(x[0]) + int(x[2])) def minus(x): print(int(x[0]) - int(x[2])) def mn(x): print(int(x[0]) * int(x[2])) def delit(x): print(int(x[0])/int(x[2])) def procent(x): print(int(x[0])/int(x[2])*100) def procent2(x): print(int(x[0])*int(x[2])/100) def stepen(x): print(int(x[0])**int(x[2])) if x[1] == "+": print(sum(x)) elif x[1] == "-": print(minus(x)) elif x[1] == "*": print(mn(x)) elif x[1] == ":": print(delit(x)) elif x[1] == "%": print(str(procent(x))+"%") print("ввод: x , % , y ") print("вывод: сколько % составляет x от y") elif x[1] == "**": print(stepen(x)) print("ввод: число, ** , степень") elif x[1] == "%%": print(procent2(x)) print("ввод: число , %% , процент") print("вывод: сколько процент составляет от числа") else: print("can't operate!")

29th Oct 2023, 8:03 PM
??????
?????? - avatar
3 Answers
+ 2
None of the defined functions return a value. The main routine tries to print the return value from each function, but of course it has none to print. I recommend replacing the print statements that are inside the functions with return statements, and let the main code do all the printing. For example, def minus(x): return int(x[0]) - int(x[2])
29th Oct 2023, 9:36 PM
Brian
Brian - avatar
+ 2
Brian The given code will print the expected result except there is a None following. I made a code about return weeks before, but now decided to expand it to cover the issue ?????? is facing. https://code.sololearn.com/cVF03G3wAXBt/?ref=app
30th Oct 2023, 4:15 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 2
Wong Hei Ming you are correct. I did not mention that, as the OP did not seem confused about why the results were being printed.
30th Oct 2023, 4:57 AM
Brian
Brian - avatar