-4

Im having some problems with my code, take a look at the main() part, i type in choice 2 and it calls open_file. But it takes 1 as else and not IF… And when i write in 1 agin it just prints 1 on the screen, what am i doing wrong? Python Version 3.4.2.

import sys
name=input("What is your name? : ")
print ("Welcome " + name)


def main():
    print ("What do you want to do")
    print ("1) Open A File")
    print ("2) open Web Browser")
    print ("3) Exit OS")

    main_choice=input()

    if main_choice == 1:
        open_file()
    elif main_choice == 2:
        web_browser()
    elif main_choice == 3:
        exit_os()
    else:
        unknown_number()


def unknown_number():
    print ("The choice you made does not exist, please choose a valid option")
    main


def open_file():
    print ("What do you want to do?")
    print ("1) Open A File TXT FILES ONLY")
    print ("2) Back to main menu")

    open_file_choice=input()

    if open_file_choice == 1:
        open_file_confirm()
    elif open_file_choice == 2:
        main()
    else:
        unknown_number()

def open_file_confirm():
    file=open("","r")
    print ("What is the file name? Include extension")
    file=input()

main()
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
GameRight
  • 1
  • 3
  • 1
    Aside: you may find [this question](http://sopython.com/canon/8/prompting-the-user-for-input-until-you-get-a-valid-response/) useful reading. (PS: `main` by itself doesn't do anything-- you want `main()` -- but that may not be a good idea to start with, as explained in the link.) – DSM Jan 02 '15 at 21:31
  • Note that you can accept the best answer to your question by clicking the tick outline to the left of their post, no matter what your reputation. – Qantas 94 Heavy Jan 04 '15 at 14:30

1 Answers1

7

input() produces a string. You need to convert that to an integer first:

main_choice = int(input())

or compare against strings instead.

You probably want to look over Asking the user for input until they give a valid response for more tips.

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343