-4

I have a piece of code here that is supposed to loop until the user types in a 0. However it does not loop more than once.

 while temp==1:
        cachv = input("Enter CACH value: ")
        mmaxv = input("Enter MMAX value: ")
        inpuv = [cachv, mmaxv]
        inpuvd= np.reshape(inpuv, (-1, 2))
        test = clf.predict(inpuvd)
        if test == 0:
            print('The performance will be high')
        else:
            print('The performance will be low')
        temp = input('Retest? 1 for Y, 0 for N: ')
    print('done')

The temp variable is first set to 1. When i enter 1 to retry it does not retry

Enter CACH value: 1
Enter MMAX value: 2
The performance will be low
Retest? 1 for Y, 0 for N: 1
done

This is what it looks like

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
  • 1
    `input` returns a string. `'1'` is not `==` to `1` in Python. – Robin Zigmond Jun 21 '20 at 18:08
  • When you assign `temp` using `input` it is a string, not an integer. So the comparison of the while loop fails. Simply change to `int(input())` – Koralp Catalsakal Jun 21 '20 at 18:09
  • @RobinZigmond Thanks guys for the quick response – Noiderate ana Jun 21 '20 at 18:11
  • 1
    Welcome to Stack Overflow. [Please don't add "solved" to your title or question](https://meta.stackexchange.com/a/116105/248627). Instead, [mark an answer correct by clicking on the checkmark](https://meta.stackexchange.com/q/5234/248627). You can also [add your own answer](https://stackoverflow.com/help/self-answer) and accept it if none of the ones you received solved your problem. – ChrisGPT was on strike Jun 21 '20 at 18:20

2 Answers2

0

The function input() returns a string object, which means that your temp variable becomes a string. therefore you are testing "1" == 1 which is false

You should update your temp variable as follows:

temp = int(input(" ... "))
Avandale
  • 242
  • 1
  • 7
0

Change temp = input('Retest? 1 for Y, 0 for N: ') to temp = int(input('Retest? 1 for Y, 0 for N: '))

Jayant Jeet Tomar
  • 167
  • 1
  • 1
  • 14