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.
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.
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.
Here is an idea
{age, _} = IO.gets("enter age: ") |> Integer.parse
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With