+ 2
Variables on Python
Hello guys, I am learning Python, I would ask you how Can I declaration kinds ( string, decimal, integer...) of variables.. And then, I Would ask if how Can I add a valour into a variable that the user insert it like input.... Do you help me? ( sorry for my bad English, I am studying also this language✌)
35 Réponses
+ 7
You're welcome 😊
Happy learning to you!
+ 4
You also should use:
if ...
elif...
elif...
elif...
+ 4
And there are not needed parenthesis. This won't cause any trouble, it's just useless
scelta = input(...)
+ 4
You also need to convert your second and third inputs to integer type with int()
and you still don't use elif + have not indented properly.
+ 4
Good job 👍
+ 4
You must write it on 3 separated lines
4
5
addizione
+ 3
Nothing's easier !
myvar = 7
this way, you declared the variable myvar, and assigned it a value of 7 at the same time. Python automatically sets it as an integer.
Same goes for other types:
myvar2 = 'tree'
you just delcared a string.
and so on.
If you really need to declare a variable first, without assigning it any value, since python 3.6, there is:
myvar : int
(...)
myvar = 7
I suggest you to start learning Sololearn's Python course where it is well explained : )
https://www.sololearn.com/Course/Python/
About your second question, on sololearn, you can assign user input to a variable this way :
myvar = input()
type of your variable will then be string. You'll need to turn it to an integer if that's your need (myvar = int(input()))
+ 3
You need to add " " to your strings:
If... =="divisione":
+ 3
And there are unneeded parenthesis. This won't cause any trouble, it's just useless
scelta = input(...)
+ 2
Variable type is defined by the value it is assigned, you don't need to use any datatype headings like in Java or C++.
But I didn't understand your second question.
+ 2
Niko Python's input function returns all the input as string, programmer might add some functionality to manipulate the returned string like wanted, there might be some builtin (__bultins__) functions or methods to perform some simple things or programmer can define his own ways to produce the input.
+ 2
Niko Your example in C++ I would translate as:
print("you insert first number")
NameVar = int(input())
But it's difference is that an error occurs, if user types a non integer value.
It would require few extra lines to handle it.
+ 2
Niko input returns strings, strings can be changed to integers with int function (constructor), int(string), but it raises an error, if string has a non int. int("74") would be ok and return 74, but int("abc") would return an error.
+ 2
Niko "elif" means "else if"
+ 2
In SoloLearn you need a newline (in input event) for each input function.
+ 2
Thank you so much! Now my code work without problems!!
This is my first code in Python😁 Thanks very much for your help! 😘
+ 1
Thank you so much for replied me!
Generally now I use VisualBasic ( I say also little C++) and I want also learn Python. I am studying now the basic, and thanks your answer I understood also declaration of variables on Python. I thought that , like C++ , there was instruction for this.
Now, I ask you, If for example, I don't say value of variable and , with input instruction, the user insert this within, how Can I do this?
For understand better:
How Can I translate this instruction in Python?
NameVar = Val(inputbox("You insert first number"))
Or C++
cout<<"you insert first number"<<endl;
cin>> NameVar;
+ 1
So, for example, do this instuction
NameVar = input("you insert first number") , is not correct ?
On Python is not possible that the user insert like input a valor?
If I would write a code for do a addiction of two numbers that thr user insert, how can I do this on Python?
+ 1
You are using a weird syntax