+ 2

need help / Python / Messaging app / Learn path

Hello, I'm having the following problem... (In the Python learning path, there is an exercise called "messaging app") and when I enter the code: Python name = input("Please enter your name: ") age = input("Please enter your age: ") print(name) print(age) (in a Python editor, not here in the SoloLearn web app), my code works perfectly fine... It asks me for the name and then the age, and then displays both in the console... Here on SoloLearn, it doesn't accept it? Am I misunderstanding the task? Or what do I need to do? It would be great if someone could help me understand where my mistake is!" i finaly make it work with this code: name = input() age = input() if name == "Stacey": print("Stacey") if age == ("34"): print ("34") if name == "Michael": print("Michael") if age == ("26"): print ("26") if name == "Anna": print("Anna") if age == ("23"): print ("23") but thats only right in the sololearn web app.. in normal python programm i cant run the code.. the code dont ask me to write something in it or press something o0 why is this correct in sololearn web? and wrong in normal python?!???

31st Jan 2025, 8:20 PM
Paincake
Paincake - avatar
6 Respuestas
+ 3
SoloLearn considers anything displayed on the user's screen as output. For example, in the code below: name = input("Please enter your name: ") age = input("Please enter your age: ") The prompts "Please enter your name: " and "Please enter your age: " appear on the screen, so SoloLearn treats them as output. However, since they are not required for the task, they cause the solution to be marked incorrect. To resolve this, simply remove the prompts: name = input() age = input() print(name) print(age) This version will work correctly and be accepted as the correct solution. Hope this answers your question!
1st Feb 2025, 12:49 AM
Amine Amllal
Amine Amllal - avatar
+ 2
As an extra tidbit on what Amine Amllal posted, You can convert inputs to integers immediately if you can assure only a number will be entered. age = int(input()) In real world situations, this can be more troublesome. One of the cardinal rules of programming is "Never trust user input". If you are not generating the input yourself, always validate anything provided by someone else before using it.
1st Feb 2025, 9:40 AM
Shardis Wolfe
+ 1
Wow, thank you for the quick help! I copied and tried out the code you provided... (for some reason, the first 4-5 times I always got an error... after a while, the same code then worked 0o I will keep in mind for later tasks that the console on SoloLearn sees everything as output... (it would have made things a lot easier for me in the next two tasks :'). but just for me: was my code right? (in normal python it works..) Thanks again and have a nice weekend! Kind regards, Paincake
1st Feb 2025, 1:22 AM
Paincake
Paincake - avatar
+ 1
Paincake for the line age = int(input()) It does not force the user to enter integers. It takes the string returned by input() and immediately tries to convert it to an integer. If a non-integer is given as input, it will raise a ValueError exception. That is why I followed up with the comment about validating input before using it. You could either put the line inside a try-except block to handle possible exception, Or validate the string from input() before converting with int().
1st Feb 2025, 12:01 PM
Shardis Wolfe
0
Your code gets the job done, but that’s not the objective of the exercise. It takes a brute-force approach, making it much longer and more complex than necessary—it’s as if you’ve bypassed the problem rather than solving it efficiently.
1st Feb 2025, 3:29 AM
Amine Amllal
Amine Amllal - avatar
0
I'm going to try it out later and see what it looks like in the console in Python :) If I understand correctly, this forces the user to enter integers? So it would not accept text, for example? And thank you very much for the help! (I've just started with Python and I still have a lot to learn )"
1st Feb 2025, 9:48 AM
Paincake
Paincake - avatar