Adding Variables - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Adding Variables (/thread-19872.html) |
Adding Variables - absolum - Jul-17-2019 Hello! I am working on a program for guitar. This is a rather simple problem but I am new to coding and don't know why the heck this is not working. I tried two ways, as you can see below. Any help is appreciated. Thanks! E_String = input('What are you fretting on the low E string? ') A_String = input('What are you fretting on the A string? ') D_String = input('What are you fretting on the D string? ') G_String = input('What are you fretting on the G string? ') B_String = input('What are you fretting on the B string? ') e_String = input('What are you fretting on the high e string? ') all_string_sum = (E_String + A_String + D_String + G_String + B_String + e_String) if all_string_sum == '1': print ("E")Method #2 if E_String + A_String + D_String + G_String + B_String + e_String == '1': print("D+") RE: Adding Variables - abi17124 - Jul-17-2019 I'm assuming the goal of your program is to add all inputs, to see if you get a total value = 1, if this is the case; All your inputs are of a string datatype and not what they're supposed to be, which is integers, since you're adding them. Lets say the user gives all your variables these values as inputs E_String = "1" A_String = "0" D_String = "0" G_String = "0" B_String = "0" e_String = "0"Since input() accepts it as a string, your variable all_string_sum is essentially adding "1" + "0" + "0"...etc to give a combined string of "100000". What you need to do is convert your input to accept user values as integers, which you can do by modifying your code as such: E_String = int(input('What are you fretting on the low E string? ')) A_String = int(input('What are you fretting on the A string? ')) D_String = int(input('What are you fretting on the D string? ')) G_String = int(input('What are you fretting on the G string? ')) B_String = int(input('What are you fretting on the B string? ')) e_String = int(input('What are you fretting on the high e string? ')) all_string_sum = (E_String + A_String + D_String + G_String + B_String + e_String) if all_string_sum == 1: print ("E")Good rule of thumb when debugging is to check what your variable holds, by printing it. RE: Adding Variables - jefsummers - Jul-17-2019 I play guitar as well. Not sure what you are trying to do here - adding up the finger positions, so a C chord would be 0+3+2+0+1+0=6, an A chord would be 0+0+2+2+2+0 = 6? You throw me by the comparison to the value 1, as there is no 6 string chord that would have that value. Even an Em7 would be 2. Agree with the above, but in the larger scheme, what are you trying to do? RE: Adding Variables - absolum - Jul-18-2019 (Jul-17-2019, 08:15 PM)jefsummers Wrote: I play guitar as well. Not sure what you are trying to do here - adding up the finger positions, so a C chord would be 0+3+2+0+1+0=6, an A chord would be 0+0+2+2+2+0 = 6? You throw me by the comparison to the value 1, as there is no 6 string chord that would have that value. Even an Em7 would be 2. Hey Man! That was a ruse, but nice to see another player here! Not that it is at all uncommon. While I did actually write out a system that would work using the "fret sum" method, ( ) it is not at all efficient and does not work in harmony with the previous layer of code I have written. Thank you! I teach music :) Hey dude, what about an A Dominant 5 chord? Doesn't have to be all 6 strings to have a sum of one. :) |