+ 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✌)

14th Feb 2019, 6:04 PM
Niko😉
Niko😉 - avatar
35 Antworten
+ 7
You're welcome 😊 Happy learning to you!
14th Feb 2019, 9:31 PM
Cépagrave
Cépagrave - avatar
+ 4
You also should use: if ... elif... elif... elif...
14th Feb 2019, 8:33 PM
Cépagrave
Cépagrave - avatar
+ 4
And there are not needed parenthesis. This won't cause any trouble, it's just useless scelta = input(...)
14th Feb 2019, 8:39 PM
Cépagrave
Cépagrave - avatar
+ 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.
14th Feb 2019, 8:50 PM
Cépagrave
Cépagrave - avatar
+ 4
Good job 👍
14th Feb 2019, 9:06 PM
Cépagrave
Cépagrave - avatar
+ 4
You must write it on 3 separated lines 4 5 addizione
14th Feb 2019, 9:10 PM
Cépagrave
Cépagrave - avatar
+ 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()))
14th Feb 2019, 6:52 PM
Cépagrave
Cépagrave - avatar
+ 3
You need to add " " to your strings: If... =="divisione":
14th Feb 2019, 8:30 PM
Cépagrave
Cépagrave - avatar
+ 3
And there are unneeded parenthesis. This won't cause any trouble, it's just useless scelta = input(...)
14th Feb 2019, 8:39 PM
Cépagrave
Cépagrave - avatar
+ 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.
14th Feb 2019, 7:25 PM
Seb TheS
Seb TheS - avatar
+ 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.
14th Feb 2019, 8:04 PM
Seb TheS
Seb TheS - avatar
+ 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.
14th Feb 2019, 8:13 PM
Seb TheS
Seb TheS - avatar
+ 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.
14th Feb 2019, 8:16 PM
Seb TheS
Seb TheS - avatar
+ 2
Niko "elif" means "else if"
14th Feb 2019, 8:38 PM
Seb TheS
Seb TheS - avatar
+ 2
In SoloLearn you need a newline (in input event) for each input function.
14th Feb 2019, 9:10 PM
Seb TheS
Seb TheS - avatar
+ 2
Thank you so much! Now my code work without problems!! This is my first code in Python😁 Thanks very much for your help! 😘
14th Feb 2019, 9:16 PM
Niko😉
Niko😉 - avatar
+ 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;
14th Feb 2019, 7:56 PM
Niko😉
Niko😉 - avatar
+ 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?
14th Feb 2019, 8:12 PM
Niko😉
Niko😉 - avatar
14th Feb 2019, 8:27 PM
Niko😉
Niko😉 - avatar
+ 1
You are using a weird syntax
14th Feb 2019, 8:49 PM
Seb TheS
Seb TheS - avatar