My vip variable does not take False as a value and always defaults to True - python-3.x

Below is the code to return either a VIP customer or an ordinary customer, the list does not take vip as a False value and always takes True.
Class DonutQueue():
def arrive(self,name,VIP):
self.name = name
self.vip = VIP
if self.vip==True:
self.queue2.append(self.name)
return self.queue2
else:
self.queue.append(self.name)
return self.queue
def next_customer(self):
while not self.queue2== []:
if not self.queue2==[]:
return self.queue
else:
return self.queue2
def main():
n = int(input("Enter the number of customers you want to add"))
for i in range(0,n):
name = input("Enter their name")
vip= bool(input("Are they a VIP"))
DonutQueue.arrive(name,VIP)
print (DonutQueue().next_customer())
Below is the output:
Enter the number of customers you want to add2
Enter their name John
Are they a VIP False
Enter their name Wick
Are they a VIP True
None
Why am I getting None as the output and my value always takes True when I input False.
Below is the debugger values:
i:0
n:2
name: John
vip: True

The Python input() method reads user input and returns it as string.
Python bool() will always return a True value for a non-empty string and False only if a string is empty.
In order to get correct the value for vip as a bool you have to manually check the input string
vipString = input("Are they a VIP")
vip = True if vipString == 'yes' else False

Variables in Python are case sensitive. Therefore, when you assign the input to vip, it is never used and VIP is used instead.
vip= bool(input("Are they a VIP"))
DonutQueue.arrive(name, vip) #instead of VIP

Related

State name spellcheck code not working with two word state names

I am working on vetting someone else's state spellchecker. The test data they ran seemed to work fine, but trying a different data set, it doesn't seem to be able to get past the first word "North" in a state name.
I need the code to be able to work with state names with two words.
This is the code:
import sys
!pip install pyspellchecker
from spellchecker import SpellChecker
#from google.colab import files
import pandas as pd
import io
#Implement spellcheck.
spell=SpellChecker()
for ind in newDF.index:
stateWordList = newDF['State'][ind].split()
if len(stateWordList) == 1:
#print(True)
if stateWordList[0] in spell:
pass
else:
correctState = input("'{}' is not a valid state, please enter a correct spelling:".format(stateWordList[0]))
newDF.at[ind, 'State'] = correctState
else:
misspelledState = False in (stateWord in spell for stateWord in stateWordList)
if misspelledState == True:
pass
else:
correctState = input("'{}' is not a valid state, please enter a correct spelling:".format(stateWordList[0]))
newDF.at[ind, 'State'] = correctState
Instead, it isn't seeing North WhateverState as valid, and returns:
'North' is not a valid state, please enter a correct spelling:
Does it need a condition specifically for two word names?
In your else statement, you have a logic error
else:
misspelledState = False in (stateWord in spell for stateWord in stateWordList)
if misspelledState == True:
pass
else:
correctState = input("'{}' is not a valid state, please enter a correct spelling:".format(stateWordList[0]))
newDF.at[ind, 'State'] = correctState
Let's see misspelledState = False in (stateWord in spell for stateWord in stateWordList), if all the words in stateWordList is well spelled, you are checking with misspelledState = False in (True, True, ...), the result will be False.
Then go to the if-else condition, it will go to else condition where outputs the correction message:
if misspelledState == True:
pass
else:
correctState = input("'{}' is not a valid state, please enter a correct spelling:".format(stateWordList[0]))
newDF.at[ind, 'State'] = correctState
You can use
misspelledState = all([stateWord in spell for stateWord in stateWordList])

How can I change values using the same object?

So I will have to finish a half-done code to get the desired output.
the half-done code goes as follows AND I AM NOT ALLOWED TO CHANGE THIS CODE:
class Wadiya():
def __init__(self):
self.name = 'Aladeen'
self.designation = 'President Prime Minister Admiral General'
self.num_of_wife = 100
self.dictator = True
the desired output goes as follows:
Part 1:
Name of President: Aladeen
Designation: President Prime Minister Admiral General
Number of wife: 100
Is he/she a dictator: True
Part 2:
Name of President: Donald Trump
Designation: President
Number of wife: 1
Is he/she a dictator: False
Now to get this output, I will have to use the same object which is wadiya in this case to change the values of the instance variables. Then print if it affected the previous values of Part 1. If it did, I'll have to print 'Previous information lost' otherwise I'll have to print 'No, changing had no effect in previous values.'
Now my question is, how can I change the values of the instance variables using the same object? This is what I've done, but I don't think this what the question has asked me to do. What do you think? Am I on the right track? Here's my approach:
class Wadiya():
def __init__(self):
self.name = 'Aladeen'
self.designation = 'President Prime Minister Admiral General'
self.num_of_wife = 100
self.dictator = True
def my_method(self):
print('Name of the President:', self.name)
print('Designation:', self.designation)
print('Number of wife:', self.num_of_wife)
print('Is he/she a dictator:', self.dictator)
def change_values(self, name, designation, num_of_wife, dictator):
self.name = name
self.designation = designation
self.num_of_wife = num_of_wife
self.dictator = dictator
print('Part 1:')
wadiya = Wadiya()
wadiya.my_method()
print('Part 2:')
wadiya = Wadiya()
wadiya.change_values('Donald Trump', 'President', 1, False)
wadiya.my_method()
Question is a bit ambiguous why would you want to change all values of an instance. If you want you can reassign new instance to same variable just pass arguments to init instead of change_method
if you want default values to class then you don't need to do init and then change values.
def __init__(self, name: str = None): # None is default value
self.name: str = name if name else 'Aladeen'
For some reason if you want to change values of instanced objects then do
wadiya.name = 'Donald'
what you are doing will work, but generally not suggested

cant get my while loops working the way i want it to

i am trying to get this code to work properly where if you input a number it will revert you back to the name=input prompt and once you enter alphabetical characters and not numerical characters it will allow you to move on to the next set of code but it keeps returning you to the name = input and doesnt let you through the rest of the code
def setup():
global name
global HP
global SP
global MP
while True:
try:
name = input('can you please tell me your name? ')
name2=int(name)
if not name.isalpha==True and not name2.isdigit==False:
break
except Exception:
print('please input your name')
continue
HP = randint(17,20)
SP = randint(17,20)
MP = randint(17,20)
print('welcome ['+name+':]: to the moon landing expedition')
There is a problem at name2=int(name) This causes an exception unless you type all numbers. In turn, this triggers the Exception and loops it forever. Your while loop seems fine.
What i think you should do:
while True:
name = input('What is your name')
isnum = False
for i in name:
if i.isnumeric():
isnum = True
break
if isnum:
print('Please type your name.')
continue
break

How to have two outputs in a def

I can't add two output to def function.
I have tried many ways but it always outputs only one of them if true or false.
loan_True = True
loan_False = False
def message():
if loan_True == True:
print()
message = f"""\
Subject: Loan Application
Hello {Fn} {Ln}, {hurray}
"""
elif loan_False == False:
print()
message = f"""\
Subject: Loan Application
Hello {Fn} {Ln}, {sorry}
"""
return message
message()
Output of code that is correct and exactly what i wantThis is the email the code sends but its wrong. i want the output in the email to be the same exact in the first pic.
I want the output to be what it is assigned.
I would propose something like
def message(loan_allowed):
if loan_allowed:
return "hooray"
else
return "sorry"
I.e. use function parameters instead of global variables. If you want to use a variable in the function I would use
def message(loan_allowed):
text = None
if loan_allowed:
text = "hooray"
else
text = "sorry"
return text
I.e. first set it in function scope. And use a different name than the function.

Using loops to call recursive function

I am trying to create a recursive function that takes three parameters: name of the dictionary, name of the original (This will be the key in the dict), and name of the final (trying to determine if it is possible to reach the final from the original)
My code functions well and enters the correct if statements and everything (tested using print statements) however, instead of the function returning True or False, it returns None every time.
I determined that this is because rather than calling my recursive function with "return" I only call the name of the function. However, if I include return in my code, the function only runs with the first value from the dictionary's key.
Any and all help on this would be appreciated.
def evolve(dictname, babyname, evolvedname):
if babyname == evolvedname:
return True
elif babyname in dictname.keys():
if dictname[babyname]:
for i in dictname[babyname]:
evolve(dictname,i,evolvedname)
else:
return False
else:
return False
Collect all recursive call's results, and return True if any of them is true.
Something like:
def evolve(dictname, babyname, evolvedname):
if babyname == evolvedname:
return True
elif babyname in dictname.keys():
if dictname[babyname]:
results = [] #To collect results
for i in dictname[babyname]:
results.append(evolve(dictname,i,evolvedname))
#Check if any of them is True
for res in results:
if res==True: return True
return False #No true among childs
else:
return False
else:
return False
But I think this code can be simplified to just:
def evolve(dictname, babyname, evolvedname):
if babyname == evolvedname:
return True
return any(evolve(dictname,i,evolvedname) for i in dictname.get(babyname,[]))
Lastly, although I don't know what you are trying to do, you might get an infinite loop, this is like doing dfs but without marking any node as already explored(black) or currently exploring(gray).

Resources