Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading integers from console

Tags:

elixir

I am creating a "choose your own adventure" style game, and I am having trouble finding a decent solution for storing user answers (that are numbers) as integers.

My solution:

 age = IO.gets("enter age: ")
 n = String.strip(age)
 new_age = String.to_integer(n)

Then I have the age as an integer; however, it takes the above two functions to convert the input. There must be a better way. I tried using IO.getn but the problem I ran into there is that one has to specify the count after the prompt, but what if one does not know how large or small the number the user enters is? I cannot seem to find anything in detail regarding how to handle user input for console applications.

like image 989
Mahmud Adam Avatar asked Dec 10 '16 01:12

Mahmud Adam


People also ask

How do I read a number from the console in Python?

To read a number from console in Python input by user, you can use input() function. input() functions enables your Console Python application to read input from user. Usually input() function reads string from console. Also, Python3 does not differentiate if the input is a string or number.

How do you read an integer value in darts?

To read an integer from console in Dart, first read a line from console using stdin. readLineSync() method, and then convert this line (string) into an integer using int. parse() method.


1 Answers

Here is an idea

{age, _} = IO.gets("enter age: ") |> Integer.parse
like image 61
Ricardo Marinovic Avatar answered Oct 30 '22 02:10

Ricardo Marinovic