How to make function that takes string argument without typing " " from the user? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 1

How to make function that takes string argument without typing " " from the user?

18th Mar 2017, 9:09 AM
Hesham Emara
6 Antworten
+ 6
Could you be more specific? input() returns string by default, so you shouldn't have a problem with what the user types...
18th Mar 2017, 12:38 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 5
Can't do it. The syntax expects data in either an explicit form or otherwise it treats the argument as a variable name.
18th Mar 2017, 2:22 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 5
@merkrafter Just out of curiosity - how would that go?
18th Mar 2017, 4:09 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 1
For example: def name(word): print word name("hesham") How to make it accept writing hesham instead of "hesham"
18th Mar 2017, 12:41 PM
Hesham Emara
0
You could write another program that keeps track of all variable names. Then, if you call a function with an unknown variable name, it puts these " " around it. This would work as a kind of a preprocessor.
18th Mar 2017, 4:08 PM
merkrafter
0
Unfortunally I am not a python expert, but I think you can make a list of variable names and take a file name. Then you read the file, search for "NAME=" declarations and add the NAME to your list. If you find another word followed by parenthesis, you can interpret it as a function and then check the arguments for being a number, a variable or a possible string. This is not the perfect approach, but it could theoretically work, right? I think regular expressions could come handy.
18th Mar 2017, 4:26 PM
merkrafter