Passing a keyboard input into a function (original) (raw)
December 19, 2022, 7:18pm 1
num = input()
def f(a):
add = a + 1
return add
def p(b):
add2 = b + 3
return add2
print(p(f(num)))
So this is my code, what it is supposed to do is take a input into function f(), then add 1, then pass variable add onto function p() which adds 3, then it is supposed to print out the inputed number
Ex: num = 1
Code would print out: 5
Can yall please help me figure out if this is possible and if so, how to do this?
barry-scott (Barry Scott) December 19, 2022, 9:34pm 2
Please use the </> preformatted text button to show your code with the indentation as you wrote it.
I think the code is good except for one thing.
The input
function returns a string and you need to convert the
string into an integer.
num = int(input())
Beware that if you do not enter a number you will see an error reported.
We can show you how to handle errors if you wish as a follow up.
CoderCat (Connor) December 19, 2022, 10:13pm 3
Oh I’m really new to this forum page, let me fix the code real quick.
CoderCat (Connor) December 19, 2022, 10:14pm 4
Thanks for telling me the fix! I got it to work!
tjreedy (Terry Jan Reedy) December 20, 2022, 1:15am 5
The 2 lines after each def
line need to be indented.
CoderCat (Connor) December 20, 2022, 12:49pm 6
I am on mobile so i can’t do that at the moment.
How do i mark a post as solved?
cameron (Cameron Simpson) December 20, 2022, 11:05pm 7
Alas, I’m not sure you can. - Cameron Simpson cs@cskk.id.au