Asking for 2 numbers | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

Asking for 2 numbers

program that ask for 2 numbers, how do I go about that

27th May 2017, 10:18 PM
Talia Pacelli
Talia Pacelli - avatar
6 Answers
+ 5
In code playground, you must give all entries required by your script at once, just when it begins ( each separated by new line )... However, if you mean ask for 2 numbers at once input() call, you need split the returned string by a specific character ( "1 2" or "1,2" and so on ) Anyway, as input() returned value is always of type string, you have to cast it to number type ( int or float ) numstr = "42" numfloat = float(numstr) numint = int(numstr) ... by the way, you can define a function to auto cast string to mosr better suited number type: def number(n): try: if '.' in n: return float(n) else: return int(n) except: return None num = number("42")
28th May 2017, 10:59 AM
visph
visph - avatar
+ 4
num_1 = int(input()) num_2 = int(input()) if (num_1 + num_2) > 100: print("etc.") # If you are in the code playground you have to enter the two numbers each in its line to avoid and EOFError. EDITED: converted the input to int. This is just a demo though.
27th May 2017, 11:17 PM
Ulisses Cruz
Ulisses Cruz - avatar
+ 3
If you are doing your code in the code playground, run this code to see a possible cause and how to handle it: https://code.sololearn.com/WhiNb9BkJUVC/?ref=app
27th May 2017, 10:48 PM
Ulisses Cruz
Ulisses Cruz - avatar
+ 1
@Ulisses: input() returns a string.
28th May 2017, 2:41 AM
Igor B
Igor B - avatar
0
I'm still running into errors, I'm not sure what I'm doing
27th May 2017, 10:38 PM
Talia Pacelli
Talia Pacelli - avatar
0
what I'm suppose to do is write a program that asked for 2 numbers. and if the sum of the two numbers is greater than 100 it prints "etc."
27th May 2017, 10:59 PM
Talia Pacelli
Talia Pacelli - avatar