Reputation: 11
Error:
Traceback (most recent call last):
File "main.py", line 61, in <module>
run()
File "main.py", line 57, in run
diceRoll()
File "main.py", line 44, in diceRoll
x = sum(int(dieawnser1))
TypeError: 'int' object is not iterable
My Code:
# Imports
from random2 import randrange
import time
import os
import string
# Global Varribles
dieroll1 = range(1, 5)
dieroll2 = range(1, 7)
spots = 6
timesRolled = 0
min = 1
max = 7
# Clear Console
def cls():
os.system('cls' if os.name=='nt' else 'clear')
# Dice roll
def diceRoll():
# Local Varribles
# Start
dieawnser1 = randrange(int(min), int(max))
if timesRolled <= 1:
time.sleep(2)
cls()
print("Dice Rolled, you got", "", dieawnser1)
time.sleep(2)
# Algrthem
x = sum(int(dieawnser1))
lose = list(1, 2, 3, 4, 5)
win = list(2,3,4, 5, 6, 8, 9, 10, 12)
if x == lose:
print("You have lost")
elif x == win:
print("You have won.")
else:
print("Error")
def run():
ask = input("Do you want to role the dice? ")
if ask == "Yes":
diceRoll()
elif ask != "No":
cls()
return
run()
Thanks to anyone who helps me fix this problem, I know this involves most likely my lists or variables but I can't seem to pinpoint the problem, thanks for the help.
Explanation of the game I am trying to create:
Two dice are rolled by each player. There are six faces on each die. There are 1, 2, 3, 4, 5, and 6 spots on these faces. The total of the dots on the two upward faces is determined once the dice have come to rest. The player loses if the total is 7 or 11. (i.e. the "house" wins). The sum becomes the player's "point" if it is 2,3,4, 5, 6, 8, 9,10, or 12. You must keep rolling the dice until you "make your point" to win. (This is a user-defined option.) By rolling a 7 before making the point, the player loses.
Upvotes: 1
Views: 68
Reputation: 21
The problem is that you are passing an int object as a parameter for sum() function but sum function only accepts an iterable object(eg tuple,list , etc) As per your game rules The following code should work:
dieawnser1 = int(randrange(min, max))
dieawnser2 = int(randrange(min, max))
if timesRolled <= 1:
time.sleep(2)
cls()
print("Dice Rolled, you got", "", dieawnser1)
time.sleep(2)
# Algrthem
x = sum([dieawnser1,dieawnser2])
There are a lot more bug in the code like:
lose = list(1, 2, 3, 4, 5)
win = list(2,3,4, 5, 6, 8, 9, 10, 12)
list() accepts only one argument but you are sending multiple arguments ... Instead of using a constructor , Would it not be simpler to just do
lose = [1, 2, 3, 4, 5]
win = [2,3,4, 5, 6, 8, 9, 10, 12]
Also , is x meant to be a int object or list?? Cause, if x is int then the following line:
if x == lose:
print("You have lost")
elif x == win:
print("You have won.")
else:
print("Error")
makes no sense as you are checking a int object against a list object... you can replace the = sign with in keyword if that is what you are looking for .. if x is supposed to be a list object then there is no point in using sum funtion in x = sum(parameters) in the previous line as sum will not return a list
Upvotes: 2