Console Work Outside Godot
Lesson 27 of 48 in Coddy's Python for GDScript Developers course.
Python input reads text from standard input and removes its trailing newline; int converts numeric text. A GDScript game normally obtains input through engine APIs and events, which do not exist in ordinary Python. These lessons use a command-line contract. Python strip trims surrounding whitespace and an f-string precision produces fixed decimal output. Godot formatting can use the percent operator with an Array of values.
GDScript
extends SceneTree
func _init():
var label = " Green tea ".strip_edges()
var cents = 305
print("%s: %.2f" % [label, cents / 100.0])
quit()Python
label = " Green tea ".strip()
cents = 305
print(f"{label}: {cents / 100:.2f}")Porting trap
Do not wait for a scene-tree callback or print a prompt in the Python tasks. The supplied driver defines when your code runs and what input it receives.
Complete the task in its editable modules. The locked driver.py supplies execution; keep the required API names.
Challenge
BeginnerRead a product label on the first line and a nonnegative integer number of cents on the second. Remove surrounding whitespace from the label but preserve internal spaces. Print exactly label: dollars.cents, with two digits after the decimal point and a final newline. The label may be empty, in which case the output begins with : . Do not print a currency symbol or input prompts.
Your code goes in main.py. The locked driver.py starts your program and cannot be edited.
Try it yourself
# Starts the program in main.py. Edit main.py to solve the challenge.
from runpy import run_module
run_module("main", run_name="__main__")
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Python for GDScript Developers
1From GDScript to Python
Run Your First Python ProgramPrint Is Not printsVar, Const, and BindingsInteger Division DifferencesSimilar Indentation, New ScopeRecap: Rewrite a Receipt4Iteration and Transformations
Ranges and RebindingItems and enumerateWhile and Early ExitArray Callbacks and KeysRecap: Build a Ranking2Values and Decisions
Null and NoneThe is Keyword Changes MeaningLogical Results and OperandsConditional ExpressionsRecap: Eligibility Rules3Collections and Shared State
Array and List APIsArrays, Vectors, and TuplesDictionary Lookup RulesDuplicate and Shallow CopyRecap: Clean an Inventory6Input, Errors, and Modules
Console Work Outside GodotValidation and ExceptionsScript Resources and ImportsTyped GDScript and AnnotationsRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler