Kevin

Python Beginner Tutorial 4 - User Input and Types

Recommended Posts

This tutorial covers getting input from a user by using the Python built int input() function. This video also goes over the difference between variable types, namely integers and strings in Python. After watching this tutorial users should be able to prompt the user for input and parse integers from strings using the int() function.

Share this post


Link to post
Share on other sites

Hey dude, loving the tutorials :) . But I have one problem. I tried to run my calculator as an app (using the idol shell). It ran fine at first, but after I entered a second number and pressed enter, it simply closed. I am new to programming (like extremely), so help would be greatly appreciated. Here are some screenshots for reference. They aren't in any particular order. https://drive.google.com/open?id=0Bw8sqLZFmKRTR2dTeXBFeU5UYms

Thanks,

Gus Bateman

batemangus@gmail.com

Share this post


Link to post
Share on other sites

Hi Gus. Welcome!

Could you post your source code? It only looks like that link has one image. Remember to use the code button (looks like "< >" when posting source code on the forum).

Like this:

print("Hello World")

What is likely happening is the program is finishing and closing as a result. One a program is done (reaches its last line) it closes. For example, in my Hello World above, it will execute that one line and then exit. If I am running the program in a new window I might see it pop up and then quickly close. Try adding an input at the end of your program to keep it open.

input("Press enter to exit...")

 

Share this post


Link to post
Share on other sites

Hi, Can you please let me know how to call the main function again after executing the program.

Usually, when we get the result, the program will terminate and we need to re-run the program.

Share this post


Link to post
Share on other sites

Here is my code

def main():  #main is the entry point
    operation = raw_input('What do you want to do (+,-,*,/): ')
    if(operation != '+' and operation != '-' and operation != '*' and operation != '/'):  #You can use () to say what you want to do/ look for first
        #invalid operation
        print("You must enter valid operation.")
    else:
        var1 = int(input('Enter num1: '))
        var2 = int(input('Enter num2: '))
        if(operation == '+'):
            print(sum(var1, var2))
        elif(operation == '-'):
            print(dif(var1,var2))
        elif(operation == '*'):
            print(mul(var1,var2))
        else:
            print(div(var1,var2))
main()

 

I received 2 errors:

global name "X" is not defined. this was sum, dif, mul, and div

typeError: 'int' object is not iterable.

I was able to run the addition but unable to tdo the rest. I started playing with it and messed somthing up...

Thanks!

number4.py

Share this post


Link to post
Share on other sites

The only thing I can see wrong with it is this line

 operation = raw_input('What do you want to do (+,-,*,/): ')

which should be

 operation = input('What do you want to do (+,-,*,/): ')

assuming all the other functions are present.

Share this post


Link to post
Share on other sites

Hi guys,

I cannot run the code because error:

" if(operation == '+'):
                        ^
TabError: inconsistent use of tabs and spaces in indentation
"  appears. Do you have an idea where I made a mistake?

 

Here is the code:

def main():
    operation = input("What do you want to do?")
    if (operation != '+' or oroperation != '-' or operation != '*' or operation != '/'):
       #invalid operation
       print ("You must enter valid operation")
    else:
        var1 = int(input("Enter n1: "))
        var2 = int(input("Enter n2: "))
        if(operation == '+'):
            print(add(var1, var2))
        elif(operation == '-'):
          print(sub(var1, var2))
        elif(operation == '*'):
          print(mul(var1, var2))
        else(operation == '/'):
         print(div(var1, var2))

main()

 

Thanks in advance.

Share this post


Link to post
Share on other sites

Good evening,

while i am running on cmd I am seeking the syntax error on main(). my code is as below:

def main() :
    operation = input ("what do you wnt to do (+,-,*,/): ")
    if(operation != "+" and operation != "-" and operation != "*" and operation != "/"):
        #invalid operation
        print("You must enter a valiid operation")
    else:
        var1 = int(input("Enter num1: "))
        var2 = int(input("Enter num2: "))
        if(operation == "+"):
            print(add(var1, var2))
        elif(operation == "-"):
            print(sub(var1, var2))
        elif(operation == "*"):
            print(mul(var1, var2))
        else:
            (print(div(var1, var2))
            
main()

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...