Python simplifying if statement logic [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have a case where I have to use if statement learning python:
if ([Team1_matches[0]>Team2_matches[0] and Team1_matches[1]>Team2_matches[1] and Team1_matches[2]>Team2_matches[2] and
Team1_matches[3]>Team2_matches[3] and Team1_matches[4]>Team2_matches[4]]):
winner="Team 1"
elif ([Team2_matches[0]>Team1_matches[0] and Team2_matches[1]>Team1_matches[1] and Team2_matches[2]>Team1_matches[2] and
Team2_matches[3]>Team1_matches[3] and Team2_matches[4]>Team1_matches[4] ]):
winner="Team 2"
else:winner="it was a draw or something went wrong"
It's always returning team 1 for some reason due to my poor combination, please advice how I can achieve the real value for winner, without having to if for many lines, if this is programmatically correct I will do it, just need advice
Without the braces
if Team1_matches[0]>Team2_matches[0] and Team1_matches[1]>Team2_matches[1] and Team1_matches[2]>Team2_matches[2] and
^
SyntaxError: invalid syntax
I am getting the input this way:
Team1_matches[0] = input("Enter the score that team 1 got on match 1? ")

As Rawing said in the comments, you have unneeded braces around your checks and non-empty lists are considered True. See the Python documentation for more info.
Try this:
Team1_matches=[1,2,3,4,5]
Team2_matches=[5,5,5,5,6]
if (Team1_matches[0]>Team2_matches[0] and Team1_matches[1]>Team2_matches[1] and Team1_matches[2]>Team2_matches[2] and Team1_matches[3]>Team2_matches[3] and Team1_matches[4]>Team2_matches[4]):
winner="Team 1"
elif (Team2_matches[0]>Team1_matches[0] and Team2_matches[1]>Team1_matches[1] and Team2_matches[2]>Team1_matches[2] and Team2_matches[3]>Team1_matches[3] and Team2_matches[4]>Team1_matches[4]):
winner="Team 2"
else:
winner="it was a draw or something went wrong"
print(winner)
>>> Team 2

The solution using built-in all function:
Team1_matches = [1,2,3,4,5]
Team2_matches = [2,3,4,5,6]
r = range(0, len(Team1_matches)) # range size
if all(Team1_matches[i] > Team2_matches[i] for i in r):
winner="Team 1"
elif all(Team2_matches[i] > Team1_matches[i] for i in r):
winner="Team 2"
else:
winner="it was a draw or something went wrong"
print(winner) # outputs: "Team 2"

Related

Print if remainder of division with certain number is zero - Python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am running a big loop and I want to see the progress. I want the code to print the index of the iteration when the remainder of that number with 1e4 is zero. What I ve tried so far looks like this:
print(i if (i%1e4==0))
But it doesn seem to work. I cant use any functions such as qdtm because I am also using numba and it does not accept it. Any help?
The conditional operator requires an else value. If you don't want to print anything when the condition is false, you need to use an ordinary if statement, not a conditional expression.
if i % 1e4 == 0:
print(i)
If it really has to be a single statement, you could make use the the end argument to print() to print nothing, and then include the newline in the output you print.
print(str(i) + '\n' if i % 1e4 == 0 else '', end='')
for i in range(int(1e5)):
if (i%1e4==0):
print(i)
Outputs:
0
10000
20000
30000
40000
50000
60000
70000
80000
90000

Why this Syntax Error on Python 3.8 for Mac [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Check out the screenshot and help this newb with why i'm getting this syntax error with the for loop even though im following the right syntax.
The code :
elif choice == 'AVERAGE':
import statistics
lst = []
n = int(input('Enter number of values to calculate mean of: ')
for i in range(0,n):
ele=int(input())
lst.append(ele)
The Error : Invalid Syntax for the ':' after 'range(0,n)'
You are spacing the items inside the for loop with double Tab, the indentation should be either 4 spaces or a single tab.
And you are missing a parenthesis closing in the n input line
See the modified code below.
elif choice == 'AVERAGE':
import statistics
lst = []
n = int(input('Enter number of values to calculate mean of: '))
for i in range(0,n):
ele=int(input())
lst.append(ele)

Python: Delete a char by char from a string [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am trying to delete character by character from a string (each time a single character to see what the string is going to look like)
var = 'string'
var1 = ''
cor = []
for i in range(0, len(var)):
varl = var[:i] + var[(i+1):]
cor.append(varl)
print (cor)
This is what am getting
['t', 'sr', 'sti', 'strn', 'strig', 'tring', 'sring', 'sting', 'strng', 'strig', 'strin']
I don't know why am getting the first 5 elements in the list, they should not exist.
Does anyone know how to fix this, Thanks.
There isn't really any reason for this not to work. However, using list comprehension instead, seeing as it solved your problem:
var = 'string'
cor = [var[:i] + var[i+1:] for i in range(len(var))]
print (cor)
Returns
['tring', 'sring', 'sting', 'strng', 'strig', 'strin']
The main reason your output seems strange is the loop which should add len(var) variables max.
for i in range(0, len(var)):
In your variable definitions, you have the second variable defined as var1 (i.e. var'one'), and in your for loop, you have varl (i.e. var'el').
Change varls in your loop to var1 and you'll have what you expect.

Python Function Codecademy [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm trying to cube the number in this function, but Codecademy says it isn't returning the right result. Could anyone help?
def cube(number):
return number**number
def by_three(number):
if number % 3==0:
return cube(number)
else:
return False
Because it is not a cube. Cube is: number ** 3
Given your cube function, you are doing
n^n
for example, given n = 4, what you are really doing is 4*4*4*4
And it work only on 3 or it's multiple, given the line
if number % 3==0:
In case you are interested in one liner of this function check this out:
def by_three(number):
return number ** 3 if number % 3 == 0 else False
If you are interested in how this is done check ternary operator in python

Problems with Codecademy Python course [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Trying to teach myself Python via Codecademy. However, when I reach this section, it keeps returning the same error, no matter what I do. Anyone else encounter this problem, or know how to fix it? I'm a noob to coding of any way shape or form.
A screenshot of the error can be seen here: https://drive.google.com/file/d/0B7fG5IDRoZ3cXzZxbnlpT3RheHc/edit?usp=sharing
answer = 2
def the_flying_circus():
if ______: answer + 5 = 7
print "This gets Printed!"
elif (answer < 5):
print "As does this!"
else end
The error is:
elif (answer < 5):
^
SynxtaxError: invalid syntax
The error is in your indentation.
You can't have an elif unless it follows an if block. Because your if statement is on one line, and your next print statement is on the next line, you don't meet this requirement. I also don't know what you're intending to do with your if ________: section.
Here's a fix:
answer = 2
def the_flying_circus():
if answer + 5 == 7:
print "This gets Printed!"
elif (answer < 5):
print "As does this!"
# ...
else: end you are missing a `:`
You have a lot of other syntax errors:
if ______: answer + 5 = 7
You cannot assign to an operator.
Unless end is a variable defined somewhere using else: end is not python syntax
Not sure what you want but from your code this seems more logical:
answer = 2
def the_flying_circus():
if answer + 5 == 7:
print "This gets Printed!"
elif answer < 5:
print "As does this!"
else:
return answer

Categories