r/pythonhelp • • Aug 20 '21

Can you help me with my function?

Post image
2 Upvotes

4 comments sorted by

3

u/northernbloke Aug 20 '21

Hi, Couple of things I see wrong

  1. to test if a variable contains a INT you can use isdigit()

  2. You are trying to Add an INT to the string in your else statement. You could change it to + str(name) +

Also, are you trying to test for Strings on your Elif or an Empty Variable?

However, I am but a noob at python myself and I may be completely wrong.

1

u/FarEffect5729 Aug 21 '21

Thank you. And for the empty string, basically if the string is empty it prints 'One for you, one for me.' since there is nothing entered.

3

u/MrPhungx Aug 20 '21

So the error occurs because you can't do something like print("my String" + 5) because 5 needs to be converted to String first. You won't get the error when you do:

print("one for " + str(name) + " one for me")

That's not your Main Problem though. Your check if the string is a number is wrong. You need to do

if x.isnumeric():
    # do something with your number

2

u/robohobo- Aug 21 '21

As mentioned earlier use isdigit() to check if input is an int.

You're using x and name interchangeably just take out x altogether and put name as function parameter

I prefer f strings for formatting Print(f"name is {name}....")