This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: BUG a simple "and" and "or"
Type: behavior Stage: resolved
Components: Tests Versions: Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Samran, jameshcorbett, xtreak
Priority: normal Keywords:

Created on 2020-07-29 16:30 by Samran, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
Command Prompt - python test.py 29-Jul-20 9_16_22 PM (2).png Samran, 2020-07-29 16:30 Just a simple bug. I tried updating python and testing still can't get desired result.
Messages (5)
msg374576 - (view) Author: Samran (Samran) Date: 2020-07-29 16:30
#this is the code

from random import randint

num = randint(1,10)

print(type(num))

print(num)

ch = None                                                                                           #tried changing this tried converting types

guess = 0

print(type(guess))

print("Welcome to guessing game: ")

 

 

while ch != 'n' or ch != 'N':                                            #here is the bug this statement is not running. It works with “and”

                while( guess != num):

                                guess = int(input("Enter your guess?  "))

                                if guess == num:

                                                print("You Guessed Congratz")

               

 

                ch=input("Enter 'y' to play or 'n' to exit: ")

               

 

                if ch=='y' or ch == 'Y':

                                guess= 0

                                num = randint(1,10)

 

print("Thankyou for playing.")
msg374577 - (view) Author: James Corbett (jameshcorbett) * Date: 2020-07-29 16:57
I think this would have been a better fit for a StackOverflow issue: https://stackoverflow.com/questions/tagged/python. Also, it's not a compilation error and it doesn't have anything to do with CPython's testing framework. 

Anyway, I don't think this is a bug. For a string `ch`, it is always true that either `ch != 'n'` or `ch != 'N'`---no string is equal to both `'N'` and `'n'`. Therefore your `while` condition will always be true and the loop will always continue.

As you already noted, your loop will terminate properly if you used `and`. You could also rewrite it as `while ch not in ('n', 'N'):` which I think is clearer.
msg374578 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2020-07-29 16:57
Below is the formatted program for easier reading. In the while clause you ask for the user to enter n to exit but you check (ch != n or ch != N) so on entering "n" the first condition is false but second clause is true. For "N" it's vice-versa. You want to continue the game as long as ch is not 'n' or "N'. Hence using not(ch == 'n' or ch == 'N') will help in exiting the program. This website is for bugs related to CPython itself. Please refer to stack overflow or other mailing lists in future.

Thanks.

from random import randint

num = randint(1, 10)

print(type(num))
print(num)

ch = None  # tried changing this tried converting types
guess = 0
print(type(guess))
print("Welcome to guessing game: ")


while (
    ch != "n" or ch != "N"
):  # here is the bug this statement is not running. It works with “and”
    while guess != num:
        guess = int(input("Enter your guess?  "))
        if guess == num:
            print("You Guessed Congratz")
            ch = input("Enter 'y' to play or 'n' to exit: ")
            if ch == "y" or ch == "Y":
                guess = 0
                num = randint(1, 10)

print("Thankyou for playing.")


Fixed program : 

from random import randint

num = randint(1, 10)

print(type(num))
print(num)

ch = None  # tried changing this tried converting types
guess = 0
print(type(guess))
print("Welcome to guessing game: ")


while not (
    ch == "n" or ch == "N"
):  # Play until the ch is not n or N 
    while guess != num:
        guess = int(input("Enter your guess?  "))
        if guess == num:
            print("You Guessed Congratz")
            ch = input("Enter 'y' to play or 'n' to exit: ")
            if ch == "y" or ch == "Y":
                guess = 0
                num = randint(1, 10)

print("Thankyou for playing.")
msg374594 - (view) Author: Samran (Samran) Date: 2020-07-30 02:51
Thanks a lot. I am beginner and will try stack overflow next time. Depending on the error type. Thank you again I learned a lot.

Sent from Mail for Windows 10

From: James Corbett
Sent: Wednesday, July 29, 2020 9:57 PM
To: iamsamran1d@gmail.com
Subject: [issue41436] BUG a simple "and" and "or"

James Corbett <james.h.corbett@gmail.com> added the comment:

I think this would have been a better fit for a StackOverflow issue: https://stackoverflow.com/questions/tagged/python. Also, it's not a compilation error and it doesn't have anything to do with CPython's testing framework. 

Anyway, I don't think this is a bug. For a string `ch`, it is always true that either `ch != 'n'` or `ch != 'N'`---no string is equal to both `'N'` and `'n'`. Therefore your `while` condition will always be true and the loop will always continue.

As you already noted, your loop will terminate properly if you used `and`. You could also rewrite it as `while ch not in ('n', 'N'):` which I think is clearer.

----------
nosy: +jameshcorbett

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue41436>
_______________________________________
msg374595 - (view) Author: Samran (Samran) Date: 2020-07-30 02:55
Thanks a lot. I am beginner and will try stack overflow next time. Depending on the error type. Thank you again I learned a lot.

Sent from Mail for Windows 10

From: Karthikeyan Singaravelan
Sent: Wednesday, July 29, 2020 9:57 PM
To: iamsamran1d@gmail.com
Subject: [issue41436] BUG a simple "and" and "or"

Karthikeyan Singaravelan <tir.karthi@gmail.com> added the comment:

Below is the formatted program for easier reading. In the while clause you ask for the user to enter n to exit but you check (ch != n or ch != N) so on entering "n" the first condition is false but second clause is true. For "N" it's vice-versa. You want to continue the game as long as ch is not 'n' or "N'. Hence using not(ch == 'n' or ch == 'N') will help in exiting the program. This website is for bugs related to CPython itself. Please refer to stack overflow or other mailing lists in future.

Thanks.

from random import randint

num = randint(1, 10)

print(type(num))
print(num)

ch = None  # tried changing this tried converting types
guess = 0
print(type(guess))
print("Welcome to guessing game: ")

while (
    ch != "n" or ch != "N"
):  # here is the bug this statement is not running. It works with “and”
    while guess != num:
        guess = int(input("Enter your guess?  "))
        if guess == num:
            print("You Guessed Congratz")
            ch = input("Enter 'y' to play or 'n' to exit: ")
            if ch == "y" or ch == "Y":
                guess = 0
                num = randint(1, 10)

print("Thankyou for playing.")

Fixed program : 

from random import randint

num = randint(1, 10)

print(type(num))
print(num)

ch = None  # tried changing this tried converting types
guess = 0
print(type(guess))
print("Welcome to guessing game: ")

while not (
    ch == "n" or ch == "N"
):  # Play until the ch is not n or N 
    while guess != num:
        guess = int(input("Enter your guess?  "))
        if guess == num:
            print("You Guessed Congratz")
            ch = input("Enter 'y' to play or 'n' to exit: ")
            if ch == "y" or ch == "Y":
                guess = 0
                num = randint(1, 10)

print("Thankyou for playing.")

----------
nosy: +jameshcorbett, xtreak
type: compile error -> behavior

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue41436>
_______________________________________
History
Date User Action Args
2022-04-11 14:59:34adminsetgithub: 85608
2020-07-30 02:55:56Samransetmessages: + msg374595
2020-07-30 02:51:59Samransetmessages: + msg374594
2020-07-29 16:58:11xtreaksetstatus: open -> closed
resolution: not a bug
stage: resolved
2020-07-29 16:57:43xtreaksettype: compile error -> behavior

messages: + msg374578
nosy: + xtreak
2020-07-29 16:57:42jameshcorbettsetnosy: + jameshcorbett
messages: + msg374577
2020-07-29 16:30:45Samrancreate