+ 3
12 Respuestas
+ 3
https://code.sololearn.com/c7I5u9PQsAuG/?ref=app
+ 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.
+ 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.
+ 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 😊
+ 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. ;)
+ 1
You could try 10**(-i) instead
+ 1
gabrijel2503
Thanks, but why does the compiler give an error in my example?
+ 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
+ 1
And there we see it. :)
0
HonFu
So I don't use another literal, I'm trying to use a numeric variable 😕