-5

This is the code :

quiz = str(input("would you like to answer some questions \n choose y/n"))

quiz = quiz.lower()
while quiz != 'y' or quiz != 'n':
    print("please choose 'y' or 'n'")
    input("y/n?")

This is part of my code I have tried using str() even without or operand it is not working btw I am using python v3.7.

1) If you can please fix the code

2) If you know some other code is more efficient tell

Note: If the input is y. # for example error is " ValueError: float: Argument: y is not a number "

Hisham___Pak
  • 1,472
  • 1
  • 11
  • 23

3 Answers3

1
//This code is for explanation 

quiz = str(input("would you like to answer some questions \n choose y/n"))

quiz = quiz.lower()

while quiz != 'y' and quiz != 'n': //here you are using != that will result false this logic works fine with !

//while quiz == 'y' or quiz == 'n': //will work for or

print("please choose 'y' or 'n'")

input("y/n?")

//this code will work 

quiz = str(input("would you like to answer some questions \n choose y/n"))

quiz = quiz.lower()

while quiz != 'y' and quiz != 'n':

   print("please choose 'y' or 'n'")

   input("y/n?")

I hope this helps.

You may be interested in flow charts or Algorithms please follow this link: https://www.edrawsoft.com/explain-algorithm-flowchart.php

You may also be interested to learn about python more please follow this link: https://www.python.org/about/gettingstarted/

Thank you.

Ruchir
  • 1,018
  • 7
  • 17
  • And yes ValueError still remains is there a way to finish it – Hisham___Pak Jun 07 '19 at 19:32
  • I added the final part please copy the code below comment stating //this code will work and paste it on https://www.onlinegdb.com/online_python_compiler No value error in the shared code – Ruchir Jun 07 '19 at 19:39
0

Input looks for an integer, raw input will receive a string. Try using

raw_input(y/n)

This should clear the ValueError

Nick H
  • 1,081
  • 8
  • 13
0

Perhaps something like this?

def check_input(predicate, msg, error_string="Illegal Input"):
    while True:
        result = input(msg).strip()
        if predicate(result):
            return result
        print(error_string)

result = check_input(lambda x: x in ['yes', 'no'],
                                   'Yes or no?')
print(result)

Stolen from here

Marcus Mann
  • 321
  • 2
  • 12