Posts: 7,094
Threads: 122
Joined: Sep 2016
Run it as i show python3 menu.py .
Posts: 9
Threads: 1
Joined: Oct 2017
Ok i did it and it doesn't work :(
Posts: 1,298
Threads: 38
Joined: Sep 2016
(Oct-24-2017, 12:03 AM)pamamolf Wrote: i add it back as i was remove it when sublime text report invalid syntax for it....
Is your shebang line correctly written to something like: #! /usr/bin/env/python3 and that it is the very first line of the script?
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Posts: 7,094
Threads: 122
Joined: Sep 2016
(Oct-24-2017, 01:23 PM)pamamolf Wrote: Ok i did it and it doesn't work :( What dos not work?
I can test it in Linux mint with 3.6.
Will of course work there there as it dos in Windows where i wrote it.
# Make sure that python3 start 3.6
mint@mint ~ $ python3
Python 3.6.2 (default, Aug 6 2017, 12:55:04)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
# The script
mint@mint ~ $ cat menu.py
def solve_1():
answer = int(input('What is 2 + 2? '))
if answer == 4:
print('Correct')
input('Push enter to retun to menu')
def solve_2():
answer = int(input('What is 10 + 45? '))
if answer == 55:
print('Correct')
input('Push enter to retun to menu')
def menu():
while True:
print('(1) Solve something')
print('(2) Solve something else')
print('(Q) Quit')
choice = input('Enter your choice: ').lower()
if choice == '1':
solve_1()
elif choice == '2':
solve_2()
elif choice == 'q':
return
else:
print(f'Not a correct choice: <{choice}>,try again')
if __name__ == '__main__':
menu() Output: # Running the script
mint@mint ~ $ python3 menu.py
(1) Solve something
(2) Solve something else
(Q) Quit
Enter your choice: 788
Not a correct choice: <788>,try again
(1) Solve something
(2) Solve something else
(Q) Quit
Enter your choice: hellooooo
Not a correct choice: <hellooooo>,try again
(1) Solve something
(2) Solve something else
(Q) Quit
Enter your choice: 2
What is 10 + 45? 55
Correct
Push enter to retun to menu
(1) Solve something
(2) Solve something else
(Q) Quit
Enter your choice: Q
mint@mint ~ $
Posts: 1,298
Threads: 38
Joined: Sep 2016
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Posts: 9
Threads: 1
Joined: Oct 2017
Yes first line is:
#!/usr/bin/python3 and when i start it and add as selection menu number 7 that doesn't exist it doesn't trigger the error as it should be:
Quote:Not a correct choice: <7>,try again
Posts: 7,094
Threads: 122
Joined: Sep 2016
(Oct-24-2017, 01:49 PM)pamamolf Wrote: Yes first line is: Do not add anything to my script,just copy it and run it as as described.
Posts: 1,298
Threads: 38
Joined: Sep 2016
I have also noted from time to time, when pasting code from this site, it gets a bit messed up. Verify what your pasted code looks like is the same as the copied code.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Posts: 9
Threads: 1
Joined: Oct 2017
Oct-26-2017, 01:11 AM
(This post was last modified: Oct-26-2017, 01:11 AM by pamamolf.)
I think that the issue is that the above sample code was related to numbers but i am using as a test some commands.....
Can anyone please help to adjust it so the error function actually work?
#!/usr/bin/python3
# Import the modules needed to run the script.
import os
import subprocess
# Menu functions
def solve_1():
os.system('clear')
print("Your operating system and version is:\n")
subprocess.call(["lsb_release", "-d"])
input('\nPush enter to return to menu:')
def solve_2():
os.system('clear')
print("Just a ping for Google using -c 2 parameter:\n")
subprocess.call(["ping", "-c 2", "www.google.com"])
input('\nPush enter to return to menu:')
# Main menu
def menu():
while True:
os.system('clear')
print("Welcome,\n")
print("Please choose the menu you want to start:\n")
print('(1) Display operating system version')
print('(2) Ping a web site')
print('(Q) Quit\n')
choice = input('Enter your choice: ').lower()
if choice == '1':
solve_1()
elif choice == '2':
solve_2()
elif choice == 'q':
return
else:
print(f'Not a correct choice: <{choice}>,try again')
# Main Program
if __name__ == '__main__':
# Launch main menu
menu()
Posts: 1,298
Threads: 38
Joined: Sep 2016
O.k., here is what I think is happening. Your "clear" command to clear the screen is preventing you from actually seeing the error, it erases the screen to fast for you to see. Try commenting the "os.system('clear')" and then run your script.
Output: Welcome,
Please choose the menu you want to start:
(1) Display operating system version
(2) Ping a web site
(Q) Quit
Enter your choice: 7
Not a correct choice: <7>,try again
Welcome,
Please choose the menu you want to start:
(1) Display operating system version
(2) Ping a web site
(Q) Quit
Enter your choice:
Either don't clear the screen, or add a time.sleep() after the 'print()'.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
|