Python, True/False, integrate function into program - python

I'm relatively new to python. I want to make a program that checks if a date is valid or not. I wrote the function code to check for leap years so far. But I want to integrate into the bigger program that checks for the validity of the month and date as well. How can I say in the last line "when C is true continue with a certain if else logic (...code that I will write later)" and "when C is false continue with this other if else logic"
def leap_year(c):
if c % 4 == 0 and y % 100 != 0:
print("True (Non-Centurial)")
else:
if c % 400 == 0:
print("True (Centurial)")
else:
print("False")
pass
for c == True:
...(my if else statements)

You can implement a if-else structure using the elif keyword:
if c % 4 == 0 and y % 100 != 0:
print("True (Non-Centurial)")
elif c % 400 == 0:
print("True (Centurial)")
elif condition1:
pass
elif contition2:
pass
else
print("False")

It may be what you're looking for.
if(c): # Checks if C is True,
then… # If so, it gets in
return … # Otherwise, it goes other way
Keep in mind your leap_year function, must return something in oder to make this work

Related

How i can do simple counter?

How can I make a "counter", and after this counter reaches for example 3, I had the function executed.
When I call the function "this", 3 and more times, "if this >= 3:" don't work, and I'm not understand why does this happen.
I already tried to write it, I asked more knowledgeable people, but I haven't found a solution.
Code which i write:
# question
def get_doing():
return input('What u gonna do now, ' + name + '? ')
# doings
def doings():
do = get_doing()
this = 0
if do == 'Anything':
print('Good')
else:
print('Don\'t write, pls.')
this += 1 # Here is a problem
if this >= 3:
print('If u keep it up, you\'re going to get in trouble.')
while True:
doings()
The counter needs to exist outside the function, so that its value is remembered between calls. For example,
def doings(c):
do = get_doing()
if do == "Anything":
print("Good")
else:
print("Don't write, pls.")
c += 1
if c >= 3:
print("If you keep it up, you're going to get in trouble")
return c
c = 0
while True:
c = doings(c)

Why is my function returning None when I clearly have defined it to do otherwise? This is a basic function for calculating the area of shapes

I am currently writing a short program for my intro to computer science course, and my code is returning "none" even though I am pretty sure my definitions are clear. Don't mind the bulky naming for my functions and stuff, its a course requirement. The objective of the code is to be able to choose a shape and then directly input the required information without a written prompt, and then the program will RETURN the area of the chosen shape. I have been breaking my teeth over this for the past few hours, playing with it, but my code is returning none regardless of what I do. Any advice? Please refrain from blatantly giving me new code because I can get in trouble for that, just perhaps point me in the direction of my problem.
import math
# the following functions are built to calculate each shape
def circle_area(rad):
return math.pi*rad**2
def rectangle_area(side_one, side_two):
return side_one*side_two
def triangle_area(edge):
return (math.sqrt(3)/4)*(edge**2)
# the following function as assigned using the above functions
def shape_area():
shape_choice = input("Choose shape (1=circle, 2=rectangle, 3=triangle):")
if shape_choice == 1 or 3:
length_one = input("")
elif shape_choice == 2:
length_two, length_three = input("")
if shape_choice == 1:
circle_area(length_one)
elif shape_choice == 2:
rectangle_area(length_two, length_three)
elif shape_choice == 3:
triangle_area(length_one)
elif shape_choice != 1 or 2 or 3:
return None
I'm not sure why all my code isn't going into the specific code gray box, but I hope my code is more or less clear.
You are not returning the area values, just calculating them.
if shape_choice == 1:
return circle_area(length_one)
elif shape_choice == 2:
return rectangle_area(length_two, length_three)
elif shape_choice == 3:
return triangle_area(length_one)
Also, as #NomadMonad also mentions, the statement:
if shape_choice == 1 or 3:
is always true as 3 is a thing. Instead use if shape_choice == 1 or shape_choice == 3:
Your final elif can be an else as it is the final condition that can be returned. You could even remove it as python will return None if nothing is returned anyway.
This line
if shape_choice == 1 or 3
Always evaluates to True
You should change it to
if shape_choice == 1 or shape_choice == 3
Similarly
elif shape_choice != 1 or 2 or 3

Python For loops with while and if statements: Why doesnt it return in the first case?

Hey guys I know this question might seem dumb, but I just started. This is Python 3.7.
Any way I have written 2 versions of the code, the second one works, but I've added another while loop where I thought it wasn't needed. Why doesn't the first version work? Its iterating a list of numbers (nums).
This is from a problem statement:
Write a function that takes in a list of integers and returns True if it contains 007 in order.
spy_game([1,2,4,0,0,7,5]) --> True
spy_game([1,0,2,4,0,5,7]) --> True
spy_game([1,7,2,0,4,5,0]) --> False
My first code was this, and it always returned False:
z = 0
for i in nums:
while z < 2:
if i != 0:
break
else:
z += 1
break
if i != 7:
break
else:
return True
return False
Why does this change, make it work?
z = 0
for i in nums:
while z < 2:
if i != 0:
break
else:
z += 1
break
while not z < 2:
if i != 7:
break
else:
return True
return False
Thanks very much!
Your problem is the way you're using the while-loop. The code in the while-loop is executed as long as the condition is met. However, you call a break or return inside the while-loop in each way.
So what you actually want to use is an if-statement instead of the while-loop. Then the break would work the way you expected it to work in the first example. The break makes the program jump out of the current loop, that is the while-loop. But for the first example to work correctly, it should jump out of the higher for-loop. Using if instead of while should fix this.
Maybe try something like this:
z = 0
for i in nums:
if z < 2:
if i == 0:
z += 1
else:
if i == 7:
return True
return False

While loop that's meant to be infinite freezes after first cycle

My goal is to make a program that prints onscreen all of the prime numbers it can find, however I have this issue where the while loop runs only once, instead of repeating forever.
def isPrime(num):
if num < 2:
return False
if num == 2:
return True
if num % 2 == 0:
return False
i = 3
while i * i <= num:
if num % i == 0:
return False
i += 2
x = 1
while True:
x += 1
if isPrime(x):
print (x)
I also have tried adding print("You can see this.") at the very end of the code, and it runs, but only once.
I'm sure it's a common mistake, since Python is very strict on indentation, but could you guys help me discover it? Thanks in advance.
You need to return True at the very end, after and outside the final loop. Otherwise the function ends without explicitly returning anything, so it implicitly returns None which is interpreted as false.
The i +=2 is indented too deep, the inner loop never quits.
It should read
while i * i <= num:
if num % i == 0:
return False
i += 2
Otherwise i never increases and the loop goes on and on and on.

An entry with parentheses in Python 3.5 only continues if the Enter key is pressed twice

I am trying to solve a simple exercise to identify if the parentheses used in an equation are correct, but when I try input an equation like this a+(b*c)-2-a ,
I have to press enter twice. This only happens inside the EOF block.
What could be wrong? Thank you very much!
while True:
try:
x = input()
z = []
for y in x:
if y == '(':
z.append(y)
elif y == ')':
z.append(y)
q = ''.join(z)
d = 0
while d == 0:
if q.count('()') != 0:
q = q.replace('()', '')
else:
if q.count('(') >= 1 or q.count(')') >= 1:
print('incorrect')
else:
print('correct')
d = 1
except:
break
You have two while True loops. On the first time around, you enter while d == 0: with a valid balance of parentheses meaning that if q.count('()') != 0: is True. That while loop then breaks, because d is incremented by 1. But the print statements require if q.count('()') != 0: to be False. Therefore, you start the whole loop again due to the very first while True:, press enter (which is effectively an invalid input for your algorithm) which allows you to get to:
else:
if q.count('(') >= 1 or q.count(')') >= 1:
print('incorrect')
else:
print('correct')

Categories

Resources