0

I am trying to make a calculator and trying to have it loop. I made a new file to test it, when I run it instead of starting at while numvars =="1" it is starting at e = int(input("What exponent do you want"). This is even happening if you say no for the exponent. What exactly is the problem that is occurring?

from math import sqrt
numvars = input("how many numbers do you want? Please only pick a number 1-3. Only type out the numberical character")
switch = True
for x in numvars:
    while numvars == "1":
        if switch == True:
            a = int(input("What number would you like? Type ONLY the numberical character and not the word"))
            exponents = input("Want to use exponents? MUST BE written as 'yes' or 'no'")
        if exponents == "yes" or "Yes" or "YES" or "Y":
            root = input("Would you like to do a square root? yes or no")
            if root == "yes" or "Yes" or "YES" or "Y":
                e = int(input("What exponent do you want?"))
                b = a ** e
                c = sqrt(b)
                print(a, "was put to the power of", e, "giving the result", b)
                print(b, "was then square rooted giving", c)
            if root == "no" or "No" or "NO":
                e = int(input("What exponent do you want?"))
                b = a ** e
            print(a, "was put to the power of", e)
            print("Giving the final answer of", b)
            root = input("Would you like to do a square root? yes or no")
        if exponents == "no" or "No" or "NO" or "N":
            root = input("Would you like to do a square root? yes or no")
            if root == "yes" or "Yes" or "YES" or "N":
                b = sqrt(a)
                print(a, "was square rooted giving the answer of", b)

This issue is also happening if I have it as if numvars == "1". else also hasn't been working for me (using VisualStudio Code). Where I am wanting it to go back to is numvars = input("how many numbers do you want? Please only pick a number 1-3. Only type out the numberical character"); only reason that for x in numvars: being after the intended restart is just so I don't get an error of numvars not being properly defined (unless I have loops misunderstood, which is possible).

  • As your code is right now, if you answer `1` to the first prompt, you will get stuck in an infinite loop, since `numvars` is never modified inside the `while` loop. I'm not sure what you think `for x in numvars` is doing either, since you're not using x afterward. Can you explain what you think you code is doing? I don't think you understand how for loops and while loops work in python – Florent Monin Oct 08 '22 at 22:24
  • 1
    [Why does "a == x or y or z" always evaluate to True?](https://stackoverflow.com/questions/20002503/why-does-a-x-or-y-or-z-always-evaluate-to-true-how-can-i-compare-a-to-al) – Ignatius Reilly Oct 08 '22 at 22:51
  • @FlorentMonin The code is meant to give math equations. Which, it is doing. I showed what I have in a test file, but the bigger version (that doesn't have any loops), has multiple outcomes with ```numvars```. It has 1-3, instead of just 1. Here the a link https://github.com/MicroRay620/Calculator – Ruby Rose Oct 09 '22 at 21:14

1 Answers1

1

numvars is a string. I am quite sure you intended to have it as an int.

numvars=int(input(...))

And then

while numvars==1

instead of numvars=="1"

I see that you know how to convert a string to a int, since you did it several time in your code. So that is why I am only "quite sure" this one should be an int too. Because if it is a string, then

for x in numvars

iterates x among characters of numvars. For example if user types 132 for numvars, then x will be '1' then '3' then '2'. Again, quite sure that is not a wanted behavior.

Also, I fail to see why you have a while numvars==1 (or "1" that is not the point). Since you never change numvars from the start, either it is not 1, and your program will do nothing, either it is 1, and your program will loop forever. Either cases, probably not what you want.

Last remark:

if root=="no" or "No" or "NO":

certainly does not what you want. If root is "no", well, then this is true and does what you want. But in any other cases, root=="no" will evaluate as False, so the second term of the "or" operator is checked. That second term is the string "No", which is considered True. A or B is A if A is not a False value (None, False, 0, [], ...), or else it is B. So root=="no" or "No" aka (root=="no") or ("No") is True if root=="no" is, or else it is "No".

It doesn't mean (root=="no") or (root=="No")

chrslg
  • 9,023
  • 5
  • 17
  • 31
  • In the complete version (the code I have here is in a test), there is different sections where numvar equals 2 and 3. Only did the loop stuff in the test file to see if it will work. – Ruby Rose Oct 09 '22 at 21:07
  • For some reason I can't edit my previous comment, so here is a link to the [full version](https://github.com/MicroRay620/Calculator) – Ruby Rose Oct 09 '22 at 21:23