I'm new to coding, and I'm trying to write a program to create a little online store through the terminal. Here is the code:      global balance               def homescreen():         print(&quot;What would you like to do?&quot;)         print('1. Sign up')         print(&quot;2. Login&quot;)         option = int(input())         if option == 1:             sign_up()         elif option == 2:             login()         else:             print(&quot;Invalid option&quot;)             homescreen()               def sign_up():         global username         global password         username = input(&quot;Enter a username: &quot;)         password = input(&quot;Enter a password: &quot;)         homescreen()               def login():         user = input(&quot;Enter your username: &quot;)         passw = input('Enter your password: ')         if user == username and passw == password:             print(&quot;Welcome&quot;)             homepage()         else:             print('Incorrect password or username; please try again')             login()               def homepage():         print(&quot;Welcome, what would you like to do?&quot;)         print('1. Shop')         print(&quot;2. Check Balance&quot;)         print(&quot;3. Add money&quot;)         option = int(input())         if option == 1:             shop()         elif option == 2:             check_balance_page()         else:             print(&quot;Invalid option&quot;)             homepage()               def check_balance_page():         balance = 0         print(&quot;Your balance is: &quot;, balance)         print('What would you like to do?')         print('1. Go to homepage')         print('2. Add money to balance')         opt = int(input())         if opt == 1:             homepage()         elif opt == 2:             add_money()         else:             print(&quot;Invalid option&quot;)             check_balance_page()               def add_money():         print(&quot;How much money would you like to add?&quot;)         amount = int(input())         balance += amount         homepage()               def shop():         print('x')               homescreen()    Whenever I run this, I create sign up, login, and enter the check balance screen and then the add money screen. But when I enter an amount to add to the balance, i get this error message (XXXXXX represents my user path):      Traceback (most recent call last):       File &quot;C:\Users\XXXXXX\Documents\pycharm projects\main.py&quot;, line 78, in &lt;module&gt;                File &quot;C:\Users\XXXXXXXX\Documents\pycharm projects\main.py&quot;, line 8, in homescreen         option = int(input())       File &quot;C:\Users\XXXXXXXX\Documents\pycharm projects\main.py&quot;, line 21, in sign_up         username = input(&quot;Enter a username: &quot;)       File &quot;C:\Users\XXXXXXXXXXXX\Documents\pycharm projects\main.py&quot;, line 10, in homescreen         sign_up()       File &quot;C:\Users\XXXXXXXXXXXXX\Documents\pycharm projects\main.py&quot;, line 29, in login         if user == username and passw == password:       File &quot;C:\Users\XXXXXXXXXXXX\Documents\pycharm projects\main.py&quot;, line 45, in homepage         elif option == 2:       File &quot;C:\Users\XXXXXXXXXXXX\Documents\pycharm projects\main.py&quot;, line 62, in check_balance_page         add_money()       File &quot;C:\Users\XXXXXXXXXXXXX\Documents\pycharm projects\main.py&quot;, line 71, in add_money         balance += amount     UnboundLocalError: local variable 'balance' referenced before assignment    Is there a kind soul who would tell me what's wrong with this?
1022
The following code works as expected in both Python 2.5 and 3.0:  a, b, c = (1, 2, 3)  print(a, b, c)  def test():     print(a)     print(b)     print(c)    # (A)     #c+=1       # (B) test()   However, when I uncomment line (B), I get an UnboundLocalError: 'c' not assigned at line (A). The values of a and b are printed correctly. This has me completely baffled for two reasons:   Why is there a runtime error thrown at line (A) because of a later statement on line (B)? Why are variables a and b printed as expected, while c raises an error?   The only explanation I can come up with is that a local variable c is created by the assignment c+=1, which takes precedent over the "global" variable c even before the local variable is created. Of course, it doesn't make sense for a variable to "steal" scope before it exists.  Could someone please explain this behavior?
64012
I have a Python script which I would like to execute at every startup. I can run it by adding this to the startup applications:  python3 /path/to/script.py   That works, but it doesn't open a terminal window, so I can't see the program's output. How could I make it open a terminal window and execute the script in there?  Note: I get the window to stay open with input(' ') at the end of the Python script. Thanks!
53640
I did find this answer here:   However none of the commands listed worked for me. Exit, cntrl + [, cntrl + q  I'm totally a terminal newb, trying to learn how to setup git using homebrew. Watching this tutorial and I was able to set my global username and email, but I'm stuck in VIM.  I can get out of it by quitting the terminal, but I have to do this every time :(  Is there a better way?
120849
I don't know which buttons I have to click or what key combination to type to calculate a cube root.
85724
This code is part of a larger code that generates an Arithmetic Quiz, although the code I have show below just saves the score that the user got on the test and their name. When saving the code it checks if the user has taken the quiz before. If the user has taken the quiz before it saves it as 'Older Score' or 'Oldest Score', however if the user has not taken the quiz before it generates a new row for them. Additionally it works out the average score and highest score for all users it has the data for. For my task I'm supposed to make the code as short as possible.  name = ""+first_name+" "+last_name rows_data = [] class_file_names = ['Class 1.csv', 'Class 2.csv', 'Class 3.csv'] scan = open(class_file_names[int(math_class)-1], 'r+') rows = csv.reader(scan) for row in rows:     rows_data.append(row) scan.close() class_file_names=['Class 1.csv', 'Class 2.csv', 'Class 3.csv'] csvfile=open(class_file_names[int(math_class)-1], 'w', newline="") fieldnames = ['Student Name', 'Oldest Score', 'Older Score', 'Latest Score', 'Average', 'Highest'] writer = csv.DictWriter(csvfile, fieldnames=fieldnames) writer.writeheader() nameFound=False for x in rows_data[1:]:     if x[0] == name:         x[3]=x[2]         x[2]=x[1]         x[1]=str(score)         x[4]=(int(x[1])+int(x[2])+int(x[3]))/3         x[5]=max(x[1],x[2],x[3])         nameFound=True     writer.writerow({'Student Name': x[0], 'Oldest Score': x[3], 'Older Score': x[2], 'Latest Score': x[1], 'Average': x[4], 'Highest' : x[5]})  if nameFound==False:     writer.writerow({'Student Name': name, 'Latest Score': score, 'Older Score': '0', 'Oldest Score': '0', 'Average': score, 'Highest': score}) csvfile.close() input("Test complete! Press anything to close.")
55914
I'm trying to put together an add-on that contains two operators.  I've gotten each operator file to run properly independently, but I'm having trouble figuring out how to write an init.py file that would allow me to distribute them as a single add-on.  I've posted everything on Github.  The three files aren't too long:   I've read a few other threads, but nothing seems to work for me.  How can I get my init.py to register my operators properly?
45219
There are a couple of times I have been presented with some codes as solutions to problems. My question is how do write these type of scripts myself for some fairly routine tasks?
1878
How do I have a Python script that a) can accept user input and how do I make it b) read in arguments if run from the command line?
35387
Is there a goto or any equivalent in Python to be able to jump to a specific line of code?
94156
Open a prompt, try to do this:   type this code: export PS1="\e[1;30m-30-\e[m &gt;" copy it with your mouse (select the code + copy it (I have auto-copy when I select with the mouse enabled)) close the prompt open a brand new prompt immediately paste it (SHIFT INS) type "HOME" key and "END" key, you'll see the cursor go to logical positions (start and end of the code) now press ENTER you prompt changes the colors immediately paste it (SHIFT INS) type "HOME" key and "END" key, you'll see the cursor go to illogical positions (offset problem)   This means if I apply colors to the prompt I can't copy/paste + change my copied code...   NB: I'm asking it here because I thought it was a problem of  but it seems it's only related to what I'm trying to assign: export PS1="\e[1;30m-30-\e[m &gt;"... and I dont see a problem. It should just change the colors of the prompt (and that's what it does actually). But it seems it does something more I dont see. Does somebody know what it could be?
121902
I have typed out the code for this mychoice.sh bash example and I keep getting the syntax error: unexpected end of file.  Am I missing something?  mychoice.sh  #!/bin/bash  E='echo -e';e='echo -en';trap ";exit" 2 ESC=$( $e "\e") TPUT(){ $e "\e[${1};${2}H";} CLEAR(){ $e "\ec";} CIVIS(){ $e "\e[25l"} DRAW(){ $e "\e%\e(0";} WRITE(){ $e "\e(B";} MARK(){ $e "\e[7m";} UNMARK(){ $e "\e[27m";} R(){ CLEAR ;stty sane;$e "\ec\e[37;44m[J";}; HEAD(){ DRAW     for each in $(seq 1 13);do     $E "   x                    x"     done     WRITE;MARK;TPUT; 1 5     $E "BASH SELECTION MENU         ";UNMARK;}     i=0; CLEAR; CIVIS;NULL=/dev/null FOOT(){ MARK;TPUT 13 5     printf "ENTER - SELECT,NEXT         ";UNMARK;} ARROW(){ read -s -n3 key 2&gt;/dev/null &gt;&amp;2      if [[$key = $ESC[A ]];then echo up;fi      if [[$key = $ESC[B ]];then echo dn;fi;} M0(){ TPUT  4 20; $e "Login info";} M1(){ TPUT  5 20; $e "Network";} M2(){ TPUT  6 20; $e "Disk";} M3(){ TPUT  7 20; $e "Routing";} M4(){ TPUT  8 20; $e "Time";} M5(){ TPUT  9 20; $e "About  ";} M6(){ TPUT 10 20; $e "Exit   ";} LM=6 MENU(){ for each in $(seq 0 $LM);do M${each};done;} POS(){ if [[ $cur == up ]];then ((i--));fi     if [[ $cur == dn ]];then ((i++));fi     if [[ $i -lt 0 ]];then i=$LM;fi     if [[ $i -gt $LM ]];then i=0;fi;} REFRESH(){ after=$((i+1)); before=$((i-!))     if [[ $before -lt 0 ]];then before=$LM;fi     if [[ $after -gt 0 ]];then after=0;fi     if [[ $j -lt $i ]];then UNMARK;M$before;else UNMARK;M$after;fi     if [[ $after -eq 0 ]] || [$before -eq $LM ];then          UNMARK; M$before; M$after;fi;j=$i;UNMARK;M$before;M$after;} INIT(){ R;HEAD;FOOT;MENU;} SC(){ REFRESH;MARK;$S;$b;cur='ARROW';} ES(){ MARK;$e "ENTER = main menu ";$b;read;INIT;};INIT while [[ "$0" != " " ]]; do case $i in     0) S=M2;SC;if [[ $cur == "" ]];then R;$e "\nS(w     )\n";ES;fi;;     1) S=M2;SC;if [[ $cur == "" ]];then R;$e "\nS(ifconfig  )\n";ES;fi;;     2) S=M2;SC;if [[ $cur == "" ]];then R;$e "\nS(df -h )\n";ES;fi;;     3) S=M2;SC;if [[ $cur == "" ]];then R;$e "\nS(route -n  )\n";ES;fi;;     4) S=M2;SC;if [[ $cur == "" ]];then R;$e "\nS(date  )\n";ES;fi;;     5) S=M2;SC;if [[ $cur == "" ]];then R;$e "\nS($e by oTo )\n";ES;fi;;     6) S=M2;SC;if [[ $cur == "" ]];then R;exit 0;fi;;  esac;POS;done;
100082
When you press a button or change a setting, this is an operation which you can also do in Python, how do you find the Python equivalent to accessing these user interface elements.   Settings (text, numbers, colors... etc) Buttons (tools you access via key bindings, menu items etc) Menus (accessed via headers, other menus or key-bindings)
11454
I tried following a few tutorials, but I get this:   How can I fix this? I added in a info.plist (I just copied and pasted another app's file, and changed around a few things.)  Also, I used chmod +x myApp to make the script executable.  P.S. I am using El Capitan
108395
All I wrote in the interpreter was as follows:  &gt;&gt;&gt; print "Hello, World!"   File "&lt;stdin&gt;", line 1     print "Hello, World!"                         ^ SyntaxError: invalid syntax   How did I even get an error? All I tried to do was run a print command.
69342
I'm using this code to let the user enter in names while the program stores them in an array until they enter an empty string (they must press enter after each name):  people = [] info = 'a' # must fill variable with something, otherwise loop won't execute  while not info.empty?     info = gets.chomp     people += [Person.new(info)] if not info.empty? end   This code would look much nicer in a do ... while loop:  people = []  do     info = gets.chomp     people += [Person.new(info)] if not info.empty? while not info.empty?   In this code I don't have to assign info to some random string.  Unfortunately this type of loop doesn't seem to exist in Ruby. Can anybody suggest a better way of doing this?
55301
i have this code but its not working it throws syntax error, unexpected '}' error  function my_custom_menu_item($items, $args) {     if(is_user_logged_in() &amp;&amp; $args-&gt;theme_location == 'primary')     {         $user=wp_get_current_user();         $name=$user-&gt;display_name; // or user_login , user_firstname, user_lastname         $items .= '&lt;li&gt;&lt;a href=""&gt;Welcome '.$name.'&lt;/a&gt;&lt;/li&gt;';         $items.= //Change Password, Logout, etc     }     elseif (!is_user_logged_in() &amp;&amp; $args-&gt;theme_location == 'primary') {         $items .= '&lt;li&gt;&lt;a href="'. site_url('wp-login.php') .'"&gt;Log In&lt;/a&gt;&lt;/li&gt;';         $items.= //Sign up, Lost Password     }     return $items; } add_filter( 'wp_nav_menu_items', 'my_custom_menu_item');
87449
I have a python app and java app. The python app generates input for the java app and invokes it on the command line.  I'm sure there must be a more elegant solution to this; just like using JNI to invoke C code from Java.  Any pointers? (FYI I'm v. new to Python)    Clarification (at the cost of a long question: apologies) The py app (which I don't own) takes user input in the form of a number of configuration files. It then interprits these and farms work off to a number of (hidden) tools via a plugin mechanism. I'm looking to add support for the functionality provided by the legacy Java app.  So it doesn't make sense to call the python app from the java app and I can't run the py app in a jython environment (on the JVM).  Since there is no obvious mechanism for this I think the simple CL invocation is the best solution.