Why Error? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
2nd Apr 2019, 10:56 PM
Solo
Solo - avatar
12 Answers
+ 3
https://code.sololearn.com/c7I5u9PQsAuG/?ref=app
3rd Apr 2019, 7:58 AM
Anna
Anna - avatar
+ 2
e is no variable. It is a pre-defined way of writing a literal for a float. Other letters will not be accepted as part of a float literal. You can write a float several ways: 1.5 .7 7. 7e4 8e-3 3e006... but somewhere adding a variable in the middle is not defined, it was never part of the deal.
2nd Apr 2019, 11:40 PM
HonFu
HonFu - avatar
+ 2
Yeah but you do it in the middle of a literal, and Python does not accept that. See, a float with e notation goes like this: There's a number, then comes an e, and then either a positive or a negative number. So you start writing your number. 1e... Python reads the e and thinks: 'Ah, this is going to be a float in exponential notation!' Then comes the -. 'Okay, negative exponent, fine...' Up to now everything is within the ruleset. Then you suddenly write an 'i'... 'Hey, what's this?... A number has to go here!!' So you get an error. Python never expects you to write a variable here, because it waits for you to finish your e-float-literal. A variable has no place here - just like you can not just randomly write letters in the middle of a number. 5+x+7 is okay. 5x7 is not.
2nd Apr 2019, 11:55 PM
HonFu
HonFu - avatar
+ 2
HonFu Thank you for your patience and clarification ☺ I thought that the compiler could somehow be deceived, now I understand that this is impossible 😊
3rd Apr 2019, 12:31 AM
Solo
Solo - avatar
+ 2
The parser has the rules by which it has to read a piece of code, down pat. What and how it reads it makes up the language. You know the rule that a variable name has to start with a letter, right? Translated into pseudo Python, probably it goes somewhat like this: if word[0] in all_letters: Do the stuff you do with names elif word[0] in all_digits: Do whatever you do to read and interpret numbers. So as long as that number isn't finished by any separator, whitespace, parentheses, semicolon, whatever, the interpretation of the number continues. And 'i is not part of the protocol. Well, but maybe you can deceive it anyway - by using an f-string in combination with eval. ;)
3rd Apr 2019, 7:07 AM
HonFu
HonFu - avatar
+ 2
Anna, HonFu BINGO! 😃
3rd Apr 2019, 9:02 AM
Solo
Solo - avatar
+ 1
You could try 10**(-i) instead
2nd Apr 2019, 11:01 PM
gabrijel2503
+ 1
gabrijel2503 Thanks, but why does the compiler give an error in my example?
2nd Apr 2019, 11:10 PM
Solo
Solo - avatar
+ 1
I suspect you can't do it because e-whatever is part of the float literal. You could also not do something like this: x=5 y = 1234x
2nd Apr 2019, 11:21 PM
HonFu
HonFu - avatar
+ 1
And there we see it. :)
3rd Apr 2019, 8:20 AM
HonFu
HonFu - avatar
0
HonFu So I don't use another literal, I'm trying to use a numeric variable 😕
2nd Apr 2019, 11:48 PM
Solo
Solo - avatar