Input/Output Operations

Levython provides simple, efficient I/O functions for console interaction and user input handling.

Output Functions

Multiple functions available for printing output to the console:

Function Description Usage
say(value) Print value with newline (recommended) say("Hello!")
print(value) Print without newline print("Loading...")
println(value) Print value with newline (alias) println("Done")

Print Examples

# Basic output
say("Hello, World!")

# Print without newline
print("Loading")
print("...")
say("Done")  # Output: Loading...Done

# Multiple values with concatenation
name <- "Alice"
age <- 25
say("Name: " + name + ", Age: " + str(age))

Input Functions

The ask() function reads user input from the console:

Basic Input

# Get string input
name <- ask("What is your name? ")
say("Hello, " + name + "!")

# Get number input
age_str <- ask("Enter your age: ")
age <- int(age_str)
say("You are " + str(age) + " years old")

Input Validation

# Validate numeric input
input <- ask("Enter a number: ")
if len(input) > 0 then
    num <- int(input)
    say("You entered: " + str(num))
else
    say("Invalid input")
end

# Loop until valid input
valid <- false
while not valid do
    answer <- ask("Type 'yes' or 'no': ")
    if answer == "yes" or answer == "no" then
        valid <- true
        say("You chose: " + answer)
    else
        say("Invalid choice, try again")
    end
end

Common I/O Patterns

Interactive Menu

say("=== Main Menu ===")
say("1. Start Game")
say("2. Settings")
say("3. Exit")
say("==================")

choice <- ask("Enter choice: ")

if choice == "1" then
    say("Starting game...")
elif choice == "2" then
    say("Opening settings...")
elif choice == "3" then
    say("Goodbye!")
else
    say("Invalid choice")
end

Progress Indicators

# Simple progress bar
for i in range(0, 10) do
    print("#")
end
say(" Complete!")

# Percentage display
total <- 100
for i in range(0, total + 1, 10) do
    say(str(i) + "% complete")
end

Formatted Output

# Table formatting
say("Name         | Age | Score")
say("-------------|-----|------")
say("Alice        |  25 |   950")
say("Bob          |  30 |   875")
say("Charlie      |  28 |   920")

# Number formatting
price <- 1234.5
say("Price: $" + str(price))

# Right-align numbers
scores <- [100, 95, 87, 92]
for score in scores do
    say("Score: " + str(score))
end

Performance Notes

  • say() and println() are equivalent - both add newline
  • print() buffers output - call say("") to flush
  • Input is always returned as string - use int() or float() to convert
  • For large output, prefer batching strings before printing
  • ask() blocks execution until user presses Enter

Error Handling

# Handle invalid conversions
input <- ask("Enter number: ")
if len(input) > 0 then
    # Convert string to number
    num <- int(input)
    if num >= 0 then
        say("Valid number: " + str(num))
    end
else
    say("No input provided")
end

# Check for empty input
response <- ask("Continue? (y/n): ")
if len(response) == 0 then
    say("Using default: yes")
    response <- "y"
end