what is invalid about this syntax? [closed] - python

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I'm writing code to simulate x number of craps games and I have some code written that may or may not work. I'm not sure yet but I can't check because when I finish editing the module a syntax error notifies me that my code doesn't make sense. The thing is though I feel like I've used this syntax hundreds of times before. The error message reads
File "<ipython-input-5-9e8dfc269e5b>", line 7
if roll=7 or roll=11:
^
SyntaxError: invalid syntax
It doesn't like the equals sign? I don't understand why python can't read this. What is wrong?
here is all of the code for the simulation if it helps
def crap(x):
from random import randint
win=0
loss=0
for i in range(x):
roll=rand(2,12)
if roll = 7 or roll = 11:
win=win+1
elif roll = 2 or roll = 3 or roll = 12:
loss=loss+1
elif roll=4 or roll=5 or roll=6 or roll=8 or roll=9 or roll=10:
rolln=randint(2,12)
while rolln =! 7 or rolln =! roll:
rolln=randint(2,12)
if rolln = 7:
loss=loss+1
elif rolln = roll:
win=win+1

= is an assignement operator, you cannot use that in if-elif conditions. Use == for equality check.
if roll == 7 or roll == 11:
Secondly you can also reduce expressions like:
elif roll==4 or roll==5 or roll==6 or roll==8 or roll==9 or roll==10:
to:
elif roll in (4, 5, 6, 8, 9 ,10):

= is used to assign values to variables. Since you're trying to test for equality, you should be using ==, e.g.
if roll == 7

Related

While Loop displaying even numbers in 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 4 months ago.
This post was edited and submitted for review 4 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I am unable to solve this.
Problem Statement:-
Make use of while loop and print all positive even numbers smaller than or equal to the number given by the user
e.g.:-
User Input: 6
Output: 2, 4, 6
I have tried this but the code is running into an infinite loop.
num = int(input("Please enter an even number: "))
i = 0
while num >= i :
if num % 2 == 0:
print(num)
i = num + 1
Thank you.
Don't use a while loop here, just use a for loop:
num = int(input("Please enter an even number: "))
for nr in range(0, num, 2):
print(nr)

Ok really struggling here Repl.it says that line 3 is incorrect because of something to do with the semicolon [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
import random
x = randint(1, 10):
if x ==(1):
print ("yay")
MathDragon.getsHit()
MathDragon.sayHealth()
else:
print ("no")
MathDragon.sayHealth
from random import randint
x = randint(1, 10)
if x == 1:
print ("yay")
MathDragon.getsHit()
MathDragon.sayHealth()
else:
print ("no")
MathDragon.sayHealth()
Python is an 'indenting language' - your tabs/4 spaces matter.
Also, didn't need a colon after x assignment plus some other mistakes - see above

If a = str(integer), do print(len(a)) and return len(a) work the same in a function? [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 3 years ago.
Improve this question
I am new to programming and have just briefly learned functions in python. I do not understand why print(len(str)) don't work the same as return len(str) in a function.
I have tried both print and return for the last statement of the function and am confused about my understanding of len(). I need some guidance, thank you! Perhaps someone can guide me as to how I can further improve my foundation as I am still pretty new to programming. Thank you!!
def numDigits(n):
#return number of digits in an integer
str_digits = str(n)
print(len(str_digits))
numDigits(833)
If I change the
print to return len(str_digits) and
numDigits(833) to print(numDigits(833)),
I get my expected answer.
What i expected:
3
Actual result:
3
None
1
2
4
3
In the first case numDigits doesn't return a value from the function, and you only print it inside the function
def numDigits(n):
#return number of digits in an integer
str_digits = str(n)
print(len(str_digits))
print(numDigits(833))
The output here is
3
None
The 3 comes from print and None comes from the function, and when you print it, it prints None
If you want to return, you need a return statement like return len(str_digits) at the end of the function like so
def numDigits(n):
#return number of digits in an integer
str_digits = str(n)
print(len(str_digits))
#Return statement
return len(str_digits)
print(numDigits(833))
The output will now be
3
3
Now the first 3 comes from print, and the second 3 comes when you print what numDigits return, which is 3

How do I solve TypeError: '>' not supported between instances of 'int' and 'list'? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
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.
Improve this question
Here's the code, I just don't know why the error keeps showing up even after reading the past answers about it.
import random
rand = random.sample(range(10), 1)
print(rand)
guess = input('Try guessing a number between 0-10! ')
for num in str(guess):
if rand > guess:
print('The number is too low! Try again!')
elif rand < guess:
print('The number is too high! Try again!')
else:
print("You got it!")
Try replacing
rand = random.sample(range(10), 1)
With
rand = random.randint(0, 10)
Your code is trying to compare a list with a string. You need to compare two of the same. Also try making guess an integer first:
guess = int(input('Try guessing a number between 0-10! '))
and removing the str(guess) - leave just guess as it is now an integer
for this problem, simply replace random.sample(range(10), 1) with random.randint(1,11) or random.choice(range(10)). random.sample returns a list. You want to return a integer, so you can do a comparison.Besides, your error description does not seem correct. It seems you should get:
Traceback (most recent call last):
File "<pyshell#11>", line 2, in <module>
if rand>guess:
TypeError: unorderable types: list() > str()
And to avoid this, use the int() function to convert your input to a number and make the other change from above.

traced my code and it seems to be in a continous loop [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
So I am trying to write a program that takes positive,negative numbers and then displays them at the end once 0 is entered,
pos1=0
neg1=0
all1=0
flt1=float(raw_input("enter a non-zero floating point number (decimals): "))
while(flt1!=0.0):
if (flt1 < 0.0):
neg1=neg1+flt1
all1=all1+flt1
elif(flt1 > 0.0):
pos1=pos1+flt1
all1+all1+flt1
print "the sum of all numbers entered is ",all1,"the sum of all positive numbers are ",pos1,
print "and the sum of all negitive numbers are ",neg1,
my issue is that when I actually trace it, ( if I take say 5.0 and trace it) I see that it gets stuck on the "elif" part of the code and doesn't actually come back to ask for another number. I am stuck on trying to figure out what I need to do for it to come back out and ask for another non-zero number. My goal is to have the user keep entering numbers til he/she enters 0 then it would take all the negatives and add them together and display them, then do the same for the positives and then display the entire sums (negs and pos) so far entering 0 works but nothing else
Edit: fixed indentation on the elif loop
just put the flt1=... line inside the loop.
also, you should generally avoid float equality comparisons, although zero is ok.
and you should use whitespace (n = 72 + 61 * x, not n=72+61*x).
pseudocode:
while True:
num = input
if num > 0.0:
do_stuff()
elif num < 0.0:
do_neg_stuff()
else:
break

Categories

Resources