Numeric variable input by keyboard

hello everybody,
I would like to input numerical values by keyboard in the terminal in a simple script in particular for base and altezza but I couldn't find out simple example, could you please help me?

let base = 4

let altezza = 2

let calcoloArea = (base * altezza)/2

What you're looking for is the readLine() function.

let userInput = readLine()! // ask the user to type something into stdin

// convert user input string into a number else terminate the program with an error 
guard let base = Double(userInput) else { 
  fatalError("expected a number got something else.")
}

// same thing as above
guard let altezza = Double(readLine()!) else {
  ...
}

Thanks Letan for your suggestion, I tried to write it in the program and I received this error: import UIKit

let userInput = readLine()! // ask the user to type something into stdin error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP,* subcode=0x0).

// convert user input string into a number else terminate the program with an error

guard let base = Double(userInput) else {

fatalError("expected a number got something else.")

}

var n = base

let userInput1 = readLine()! // ask the user to type something into stdin// same thing as above

guard let altezza = Double(readLine()!) else {

fatalError("expected a number got something else.")

}

var m = altezza

print (n*m)

what can I do? :disappointed_relieved:

So given that you're importing UIKit my guess is that you're running this in an iOS playground? Correct me if I'm wrong. If I am correct then it doesn't really make much sense to ask for user input in a playground anyway since a playground is already interactive. But if you really want to make an actual program that can ask for user input I suggest creating a new Xcode project > macOS > Command Line Tool.

thanks Letan, are the first steps with xcode, in fact I have done some program with python and I wanted to learn swift to turn them into simple applications for ipad actually the numeric input that in python is simple thing in swift seems almost impossible.
I did not understand well what should I do, a project in xcode that uses the swf file? :cold_sweat:

Going from a python command line program to a graphical iPad app isn't a one to one translation, there's a lot of stuff in between. I suggest following a tutorial to help you out, codewithchris has some stuff that might help you.

Swift command line program

Creating a command line program in Swift isn't really going to help you get to your goal of an iPad app, but none the less you may find nice to know. Also getting a Swift program to run is as easy as running a python program.

Using Xcode:

  1. Open Xcode. On the welcome screen click "Create a new Xcode project"
  2. Select macOS in the tab bar on the template select screen.
  3. Choose "Command Line Tool"
  4. Give it a name
  5. Save it somewhere
  6. Type your code in the main.swift file.

Using the terminal:

  1. Create a new file called area.swift
  2. Type your code into that file
  3. Run the command swift area.swift in your terminal

thanks Letan I really appreciated your suggestions I will try to put them in practice then I'll update you thanks again a greeting :hugs::sweat_smile::sweat_smile:

hello Letan,
these are the result I had using Xcode:Couldn't find architecture name for cpu type 3360 / cpu sub type 0Showing Recent Messages

Prepare build
note: Using new build systemnote: Planning buildnote: Using build description from memory

Showing Recent Messages
Build system information
warning: no rule to process file '/Users/silvio/Library/Developer/Xcode/DerivedData/Area-geseevxsoiwbwsfaftrchdgonkty/Build/Products/Debug/Area' of type 'compiled.mach-o.executable' for architecture 'x86_64' (in target 'Area');

and using the terminal:
Fatal error: expected a number got something else.: file /Users/silvio/area.swift, line 5
0 swift 0x000000010c6c959a PrintStackTraceSignalHandler(void*) + 42
1 swift 0x000000010c6c8d4e SignalHandler(int) + 302
2 libsystem_platform.dylib 0x00007fff68129b3d _sigtramp + 29
3 libsystem_platform.dylib 000000000000000000 _sigtramp + 2548917472
4 libswiftCore.dylib 0x000000010f8109d9 $Ss17_assertionFailure__4file4line5flagss5NeverOs12StaticStringV_SSAHSus6UInt32VtF + 25
5 libswiftCore.dylib 0x000000010f67537f $Ss17_assertionFailure__4file4line5flagss5NeverOs12StaticStringV_SSAHSus6UInt32VtF + 4293282239
6 swift 0x00000001096f423d llvm::MCJIT::runFunction(llvm::Function*, llvm::ArrayRefllvm::GenericValue) + 365
7 swift 0x00000001096fac1c llvm::ExecutionEngine::runFunctionAsMain(llvm::Function*, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator > > > const&, char const* const*) + 1004
8 swift 0x000000010895bfe4 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 52660
9 swift 0x000000010894bd35 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 7717
10 swift 0x00000001088f1965 main + 1349
11 libdyld.dylib 0x00007fff67f3eed9 start + 1
Stack dump:
0. Program arguments: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -interpret /Users/silvio/area.swift -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -color-diagnostics -module-name area

just to inform you it is a mess..:scream::hot_face:

It looks like it's working as expected from the terminal. It's just that when entering your input you didn't give it a string it could parse as a Double. We know this because this is the error message we gave in our code.

Hello Letan,
The problem is that it is not opening the terminal and ask me to enter something..:disappointed_relieved:

Terms of Service

Privacy Policy

Cookie Policy