for vs. if loop *incorrect selection* - python

Trying to create a fill in the blanks quiz.
If I use a for loop when the answer is incorrect will always return to FIRST element on the list, is there any way to bypass this? or use a different loop?
See my full code below.
IT IS NOT FINAL
Will only work on EASY answer selection.
The issue will appear when answering correctly FIRST blank(Imagine) and failing on the second one.
Any help will be highly apreciatted.
imag = '***1*** there is no heaven, It is ***2*** if you try, No hell below us, Above us only sky, ***1*** all the people living for today, ***1*** there is no ***3***, It is not hard to do, Nothing to kill or die for, And no religion too, ***1*** all the people living life in ***4***.'
imag_ans = ['Imagine', 'easy', 'heaven', 'peace']
blanks = ['***1***', '***2***', '***3***', '***4**']
def level():
print 'Please select Level? (Easy / Medium / Hard)'
global a
a = raw_input()
if a == 'Easy':
return attempts()
if a == 'Medium':
return 'Med'
if a == 'Hard':
return 'Hard'
else :
print 'Invalid option'
print '\n'
return level()
def attempts():
print 'How many attempts will you need?'
global numberofatt
numberofatt = raw_input()
try:
float(numberofatt)
except ValueError:
print "Please enter a number for attempts"
return attempts()
numberofatt = int(numberofatt)
if numberofatt <= 0 :
print 'Please enter a positive number'
return attempts()
else :
return quiz(a)
def quiz(level):
i = 0
global user_ans
global i
print 'Please fill in the blanks, you have ' + str(numberofatt) + ' attempts'
for blank in blanks:
print 'Fill in blank' + blank
user_ans = raw_input()
if user_ans == imag_ans[i]:
i = i + 1
global imag
imag = imag.replace(blank, user_ans)
print "Correct!"
print imag
else :
return att()
n = 1
def att():
if n == numberofatt :
return 'Game Finished'
if user_ans != imag_ans[i]:
global n
n = n + 1
#blank = 0
print 'Try Again'
return quiz(a)
print level()

You could use while loop:
def level():
global a
a = raw_input('Please select Level? (Easy / Medium / Hard): ')
pending = True
while pending:
if a == 'Easy':
pending = False
return attempts()
elif a == 'Medium':
pending = False
return 'Med'
elif a == 'Hard':
pending = False
return 'Hard'
else :
print 'Invalid option'
print '\n'
Something similar could be applied for quiz(). As commented, you should check how global works. Also, revise the indentation (e.g.: in att()).

Related

Run a function in the 'if statement'

When I run clear() as below it does not print the 'else' statement. It only works for the 'if' part. When I run it one indent outside, it clears without doing the print for both if and else. Please guide on where I should place it.
import random
from art import logo,vs
from game_data import data
from replit import clear
def game_question():
return random.choice(data)
def format_data(account):
account_name = account["name"]
account_description = account["description"]
account_country = account["country"]
return f"{account_name}, {account_description}, {account_country}"
def count(num_a, num_b):
if num_a > num_b:
return "a"
else:
return "b"
win = 0
play_on = False
while not play_on:
print (logo)
account_a = game_question()
account_b = game_question()
if account_a == account_b:
account_b = game_question()
num_a = account_a["follower_count"]
num_b = account_b["follower_count"]
print(f"Account A : {format_data(account_a)}")
print (vs)
print(f"Compare to Account B: {format_data(account_b)}")
ans = input("Which account has more followers? A or B: ").lower()
if ans == count(num_a,num_b):
win += 1
print ("A win")
else:
print (f"Wrong. You lose. Win = {win}")
play_on = True
clear()
The clear() function is not indented properly in else:. Try this for the if else statement
if ans == count(num_a,num_b):
win += 1
print ("A win")
else:
print (f"Wrong. You lose. Win = {win}")
play_on = True
clear()

if, elif & !=, == boolean operations only return "false" with inputs

I am creating an Among Us ripoff (for fun!) and the while True & if/elif/else statements will only return false (not An Impostor) with the inputs. I had created a list for the names and 2 random elements from the list will be chosen as An Impostor. However, whenever I input a name that is The Impostor, it will only return
(player) was not An Impostor.
Here is my code;
import sys, time, random
names = ["player1", "player2", "player3", "player4", "player5", "player6", "player7", "player8", "player9", "player10"]
print("Players: ")
for x in names:
print(x)
print('—————————————————————————')
impostor1 = random.choice(names)
impostor2 = random.choice(names)
crewmates = 8
impostors = 2
tries = 6
while True:
talk = input("Guess who The Impostor(s) are. " + str(crewmates) + " Crewmates are left. " + str(impostors) + " Impostors are left. You have " + str(tries) + " tries left.")
if talk in names:
print(talk + " was voted for.")
time.sleep(0.1)
if talk != impostor1 or talk != impostor2:
notimp = talk + " was not An Impostor. "
names.remove(talk)
for y in notimp:
sys.stdout.write(y)
sys.stdout.flush()
time.sleep(0.05)
crewmates -= 1
tries -= 1
elif talk == impostor1 or talk == impostor2:
wasimp = talk + " was An Impostor. "
names.remove(talk)
for v in wasimp:
sys.stdout.write(v)
sys.stdout.flush()
time.sleep(0.1)
impostors -= 1
else:
print("That player was either ejected or is not a valid player.")
However, whenever I put the Impostor in the input, it says it isn't An Impostor?
I think this line is the source of the problem:
if talk != impostor1 or talk != impostor2:
Let's say impostor1 is player1 and impostor2 is player2 and someone input in player1, according to Python Boolean expression operator or that if statement will evaluate like this:
if player1 != impostor1 evaluated to False because player1 is indeed equals to impostor1.
So far so good, but because the first test is a False, Python simply evaluates and returns the right side operand which may be either True or False. In your case Python will evaluate if talk != impostor2 and return True, thereafter executes the nested block.

:TypeError: argument of type 'function' is not iterable", but works when individually tested

I am a newbie to Python 2. I am trying to build a "shopping cart" Python program, but stuck at "check stock before put to cart" step.
Firstly, I have read many threads of the same problems in here, but they are seemingly of different cause from mine.
Secondly, I already separated each function and test in a different file. They worked well individually. But when joined back the check_stock(y) function, it gave the error.
I believe that the problem comes from the "in" command.
def check_stock(y): #// Problem in this function //
if y in list:
print "%s is available" % y
add_to_cart(y)
else:
print "Sorry, but %s is not available." % y
def check_finish():
y = raw_input(">")
if y == "checkcart":
print cart #check inside shopping cart
elif y == " ":
check_finish() #loop back for blank
elif y == "list":
list() #present the list
else:
while y != "ok": #"ok" = finished shopping
check_stock(y)
else:
print "Checking out..."
sorted(cart)
print "Your item(s) are %s." % cart
exit(0)
Here are the rest of the code, if it may help:
cart = []
list = ['apple', 'banana', 'cat', 'dog', 'elephant', 'flamingo', 'goofy', 'ham']
a = 0
def list():
print list #present the list
def representInt(s): #check if value is integer
try:
int(s)
return True
except ValueError:
return False
def annoyedAtError(a): #interaction for repeated mistakes
if a < 2:
print "Numbers only please"
elif 2 < a < 4:
print "Man, just do as I say, please. I have another shift tonight."
elif a == 5 :
print "Hey, seriously?"
else:
print "..."
def check_stock(y): #// PROBLEM HERE // cross-check with list if item is available
if y in list:
print "%s is available" % y
add_to_cart(y)
else:
print "Sorry, but %s is not available." % y
def add_to_cart(y):
amount = (raw_input("How many do you want to add? > "))
if representInt(amount) == False:
annoyedAtError(a)
global a
a = a + 1
add_to_cart(y)
else:
y = y + " " + amount
print "%s is added to cart" % (y)
cart.append(y)
check_finish()
def check_finish():
y = raw_input(">")
if y == "checkcart":
print cart #check inside shopping cart
elif y == " ":
check_finish() #loop back for blank
elif y == "list":
list() #present the list
else:
while y != "ok": #"ok" = finished shopping
check_stock(y)
else:
print "Checking out..."
sorted(cart)
print "Your item(s) are %s." % cart
exit(0)
def welcome():
print """\nWelcome to cyber shopping.\n
Please enter things you want to buy.
Check your cart by typing: checkcart
type "ok" when finished.
type "list" for things available for buying"""
def start():
welcome()
check_finish()
start()
You created a list named list (which you shouldn't do, because it's a built in name already), but then you also create a function named list (again, don't do this). list refers to the function now, not your list.
So when you check y in list it tries to check if the item is in the function. You can't use in on a function, hence the error. The solution is simple: use clearer names for things!!
lst = ['apple', 'banana', 'cat', 'dog', 'elephant', 'flamingo', 'goofy', 'ham']
a = 0
list is a predefined function in python, so use some other name.
First, don't name a list list! Second, the more descriptive the names of your function, the better. So here's some code with the names edited:
cart = []
groceries = ['apple', 'banana', 'cat', 'dog', 'elephant', 'flamingo', 'goofy', 'ham']
a = 0
def prntgroceries():
print groceries #present the groceries
def representInt(s): #check if value is integer
try:
int(s)
return True
except ValueError:
return False
def annoyedAtError(a): #interaction for repeated mistakes
if a < 2:
print "Numbers only please"
elif 2 < a < 4:
print "Man, just do as I say, please. I have another shift tonight."
elif a == 5 :
print "Hey, seriously?"
else:
print "..."
def check_stock(y): #// PROBLEM HERE // cross-check with list if item is available
if y in groceries:
print "%s is available" % y
add_to_cart(y)
else:
print "Sorry, but %s is not available." % y
def add_to_cart(y):
amount = (raw_input("How many do you want to add? > "))
if representInt(amount) == False:
annoyedAtError(a)
global a
a = a + 1
add_to_cart(y)
else:
y = y + " " + amount
print "%s is added to cart" % (y)
cart.append(y)
check_finish()
def check_finish():
y = raw_input(">")
if y == "checkcart":
print cart #check inside shopping cart
elif y == " ":
check_finish() #loop back for blank
elif y == "list":
prntgroceries() #present the list of groceries
else:
while y != "ok": #"ok" = finished shopping
check_stock(y)
else:
print "Checking out..."
sorted(cart)
print "Your item(s) are %s." % cart
exit(0)
def welcome():
print """\nWelcome to cyber shopping.\n
Please enter things you want to buy.
Check your cart by typing: checkcart
type "ok" when finished.
type "list" for things available for buying"""
def start():
welcome()
check_finish()
start()
Second thing of note, unrelated to your question: when you ask for the things you can buy, it prints the list...and ends the program. I would suggest doing something to prevent the program from ending right after you print the list of things available. Maybe even print that list at the very beginning.
Hope this helps!

Python: If condition appears to be met but isn't triggering

I know this seems like it should be very simple, but at this point I'm at my wit's end trying to figure this out. I've coded up a calculator in python, but for some reason the ending if-else statement is only firing the else segment.
import sys
import re
#setting values
x = 0
n = '+'
y = 0
#valid input flag
valid = True
#continue operations flag
run = True
again = "k"
#addition function
def add(x, y):
return x + y
#subtraction function
def subtract(x, y):
return x - y
#multiplication function
def multiply(x, y):
return x * y
#division function
def divide(x, y):
return x / y
#continuation loop
while run == True:
#Prompt for and accept input
equation = raw_input("Please insert a function in the form of 'operand' 'operator' 'operand' (x + y): ")
equation.strip()
#Divide input into 3 parts by spaces
pieces = re.split('\s+', equation)
#set part 1 = x as float
x = pieces[0]
try:
x = float(x)
except:
print "x must be a number"
valid = False
#set part 2 = operator
if valid == True:
try:
n = pieces[1]
except:
print "Please use valid formating (x [] y)."
valid = False
#set part 3 = y as float
if valid == True:
y = pieces[2]
try:
y = float(y)
except:
print "y must be a number"
valid = False
#If input is valid, do requested calculations
while valid == True:
if n == '+' :
print equation + " =", add(x,y)
elif n == '-' :
print equation, " =", subtract(x,y)
elif n == '*' :
print equation, "*", y, " =", multiply(x,y)
elif n == '/' :
if y == 0:
print "You cannot divide by zero."
else:
print equation, " =", divide(x,y)
else:
print "Please use an appropriate operator ( + - * / )."
#play again
again = raw_input("Play again? ")
print again
if again == ("yes", "y", "YES", "Yes","yes"):
run = True
print "yes'd"
else:
print "no'd"
run = False
When I run this code, I get two different problems:
If I enter a valid input (ie: 2 + 2), then my output is
"2 + 2 = 4.0"
"2 + 2 = 4.0"
"2 + 2 = 4.0"
repeating forever.
If I enter an invalid input, I get the "Play again? " Prompt, but
no matter what I enter, the else statement fires.
(for instance, in the case that I enter "yes" into "Play again? ", it will print:
"yes" (<-- this is from "print again" line )
"no'd" (<-- this is from "else: print "no'd" )
I dont know how to solve either of these problems at this point, so any help would be greatly appreciated.
Edit: Thank you everyone, I wish I could check mark all of you for helping me understand different things about what I did wrong.
In while valid == True:, you never change the value of valid, so it's always True and the loop is infinite. I don't see why it's even a loop - change it to if like the blocks above it and it will behave as expected.
Also, in if again == ("yes", "y", "YES", "Yes","yes"):, change == to in and it will behave as expected.
Perhaps you should replace this code:
while valid == True:
if n == '+' :
print equation + " =", add(x,y)
elif n == '-' :
print equation, " =", subtract(x,y)
elif n == '*' :
print equation, "*", y, " =", multiply(x,y)
elif n == '/' :
if y == 0:
print "You cannot divide by zero."
else:
print equation, " =", divide(x,y)
else:
print "Please use an appropriate operator ( + - * / )."
With this...
if valid:
Or...
while valid == True:
# Insert your previous code here.
break
You could also just simply set valid to false at the bottom of your loop too. That would work.
I think valid is constantly true in this case. You have also written while valid is true, which means it will keep iterating over the loop until valid is equalled to false. It appears that within this block of code in the while loop, valid isn't switched to false.
while valid == True: should probably be if valid == True
and for your second problem:
if again == ("yes", "y", "YES", "Yes","yes"): should probably be:
again = again.lower();
if again == "yes" or again == "y":
Your answer is looping because of
while valid == True:
Replace the loop with the if statement
You get "no'd" because of
if again == ("yes", "y", "YES", "Yes", "yes"):
Here you are equating string with a tuple, instead of checking whether the string is contained within a tuple. Try this instead:
if again in ("yes", "y", "YES", "Yes""):

determinating if the input is even or odd numbers

Hello I am trying to write a program in python that asks the user to input a set of numbers of 1's and 0's and I want the program to tell me if I have and even number of zeros or an odd number of zeros or no zero's at all. Thanks for your help!!
forstate = "start"
curstate = "start"
trans = "none"
value = 0
print "Former state....:", forstate
print "Transition....:", trans
print "Current state....", curstate
while curstate != "You hav and even number of zeros":
trans = raw_input("Input a 1 or a 0: ")
if trans == "0" and value <2:
value = value + 1
forstate = curstate
elif trans == "1" and value < 2:
value = value + 0
forstate = curstate
curstate = str(value) + " zeros"
if value >= 2:
curstate = "You have and even number of zeros"
print "former state ...:", forstate
print "Transition .....:", trans
print "Current state....", curstate
Looks like you're trying to do a finite state machine?
try:
inp = raw_input
except NameError:
inp = input
def getInt(msg):
while True:
try:
return int(inp(msg))
except ValueError:
pass
START, ODD, EVEN = range(3)
state_next = [ODD, EVEN, ODD]
state_str = ['no zeros yet', 'an odd number of zeros', 'an even number of zeros']
state = START
while True:
num = getInt('Enter a number (-1 to exit)')
if num==-1:
break
elif num==0:
state = state_next[state]
print 'I have seen {0}.'.format(state_str[state])
Edit:
try:
inp = raw_input
except NameError:
inp = input
START, ODD, EVEN = range(3)
state_next = [ODD, EVEN, ODD]
state_str = ['no zeros yet', 'an odd number of zeros', 'an even number of zeros']
def reduce_fn(state, ch):
return state_next[state] if ch=='0' else state
state = reduce(reduce_fn, inp('Enter at own risk: '), START)
print "I have seen " + state_str[state]
It sounds like homework, or worse an interview questions, but this will get you started.
def homework(s):
counter = 0
if '0' in s:
for i in s:
if i == '0':
counter = counter + 1
return counter
don't forget this part over here
def odd_or_even_or_none(num):
if num == 0:
return 'This string contains no zeros'
if num % 2 == 0
return 'This string contains an even amount of zeros'
else:
return 'This string contains an odd amount of zeros'
if you call homework and give it a string of numbers it will give you back the number of 0
homework('101110101')
now that you know how many 0s you need to call odd_or_even_or_none with that number
odd_or_even_or_none(23)
so the solution looks like this
txt = input('Feed me numbers: ')
counter = str( homework(txt) )
print odd_or_even_or_none(counter)
try:
inp = raw_input
except NameError:
inp = input
zeros = sum(ch=='0' for ch in inp('Can I take your order? '))
if not zeros:
print "none"
elif zeros%2:
print "odd"
else:
print "even"
The simple solution to your problem is just to count the zeros, then print a suitable message. num_zeros = input_stream.count('0')
If you're going to build a finite state machine to learn how to write one, then you'll learn more writing a generic FSM and using it to solve your particular problem. Here's my attempt - note that all the logic for counting the zeros is encoded in the states and their transitions.
class FSMState(object):
def __init__(self, description):
self.transition = {}
self.description = description
def set_transitions(self, on_zero, on_one):
self.transition['0'] = on_zero
self.transition['1'] = on_one
def run_machine(state, input_stream):
"""Put the input_stream through the FSM given."""
for x in input_stream:
state = state.transition[x]
return state
# Create the states of the machine.
NO_ZEROS = FSMState('No zeros')
EVEN_ZEROS = FSMState('An even number of zeros')
ODD_ZEROS = FSMState('An odd number of zeros')
# Set up transitions for each state
NO_ZEROS.set_transitions(ODD_ZEROS, NO_ZEROS)
EVEN_ZEROS.set_transitions(ODD_ZEROS, EVEN_ZEROS)
ODD_ZEROS.set_transitions(EVEN_ZEROS, ODD_ZEROS)
result = run_machine(NO_ZEROS, '01011001010')
print result.description

Categories

Resources