import random
from random import shuffle
import time
game = True
while game:
class X(object): # creates an object based class named X
def __init__(self, name, rating, power, defence, XRay, toughness): #defines the structures of the class so they can be later displayed
self.name = name
self.rating = rating
self.power = power
self.defence = defence
self.XRay = XRay
self.toughness = toughness
def __repr__(self):
print("\nName : ", self.name, "\nRating : ", self.rating, "\nPower : ", self.power, "\nDefence : ", self.defence, "\nXRay : ", self.XRay, "\nToughness : ", self.toughness) #Telling the code that whenever it calles out the X class to print the atributes of the Card
q = X("Inferno Scorpion", 3, 970, 940, 2010, 1090)
w = X("Grandmaster Sub-Zero", 3, 900, 1000, 1900, 1000)
e = X("Kenshi", 3, 910, 930, 1840, 950)
r = X("Master Of Souls Ermac", 3, 940, 850, 1790, 950)
t = X("Sun God Kotal Kahn", 3, 930, 950, 1040, 890)
y = X("Jax Briggs", 2, 500, 520, 1020, 800)
u = X("Cassie Cage", 2, 520, 380, 900, 400)
i = X("Sonya Blade", 2, 460, 370, 830, 380)
i2 = X("Tactical Sonya Blade", 3, 930, 840, 1770, 930)
o = X("D'vorah", 2, 460, 390, 850, 370)
p = X("Kung Jin", 2, 410, 360, 770, 330)
p2 = X("Bojutsu Kung Jin", 3, 860, 880, 1740, 930)
a = X("Sergent", 1, 210, 230, 440, 230)
s = X("Oni", 1, 250, 260, 510, 200)
d = X("Monk", 1, 230, 210, 440, 200 )
f = X("Osh-Tekk", 1, 220, 220, 440, 200)
g = X("Trooper", 1, 200, 200, 400, 210)
j = X("Kano", 2, 400, 380, 780, 420)
k = X("Shirai Ryu", 1, 220, 180, 400, 200)
l = X("Reptile", 3, 780, 760, 1540, 890)
z = X("Johnny Cage", 2, 390, 420, 810, 360)
x = X("Lin Kuei", 1, 180, 220, 400, 200)
c = X("Saurian", 1, 210, 190, 400, 200) # the playing cards
allCards = [q, w, e, r, t, y, u, i, o, p, a, s, d, f, g, j, k, l, z, x, c]
shuffle (allCards)
print ("Your deck is being dealt... please wait")
time.sleep(1)
print ("...")
time.sleep(1)
half = int(len(allCards)/2)
p1 = (allCards[0:half])
cpu = (allCards[half:])
print ("\nPlayer one has",len(p1), "cards... let's do this!")
def error():
global p1
global cpu
chosenability()
def chosenability ():
global p1
global cpu
pick=input("\nMake your selection: \n")
user_choice=pick.lower()
if user_choice=="Rating":
p1_choice=p1[0].rating
cpu_choice=cpu[0].rating
print ("\nRating = ", p1_choice)
elif user_choice=="Power":
p1_choice=p1[0].power
cpu_choice=cpu[0].power
print ("\nPower = ", p1_choice)
elif user_choice=="Defence":
p1_choice=p1[0].defence
cpu_choice=cpu[0].defence
print ("\nDefence = ", p1_choice)
elif user_choice=="XRay":
p1_choice=p1[0].XRay
cpu_choice=cpu[0].XRay
print ("\nXRay = ", p1_choice)
else:
print ("\nInvalid selection")
error()
if p1_choice>cpu_choice:
time.sleep(2)
print ("Computers card is...",(cpu[0]))
time.sleep(2)
print ("\nYou win card", cpu[0].name)
p1.append(cpu[0])
del cpu[0]
p1=p1+[p1.pop(0)]
print ("You now have", len(p1),"cards")
print ("CPU now has", len(cpu),"cards")
elif p1_choice<cpu_choice:
time.sleep(2)
print ("Computers card is...",(cpu[0]))
time.sleep(2)
print ("\nYou Lose your card", p1[0].name)
cpu.append(p1[0])
del p1[0]
cpu=cpu+[cpu.pop(0)]
print ("CPU now has", len(cpu),"cards")
print ("You now have", len(p1),"cards")
else:
time.sleep(2)
print ("Computers card is...",(cpu[0]))
time.sleep(2)
print ("\nDraw")
cpu=cpu+[cpu.pop(0)]
p1=p1+[p1.pop(0)]
while len(p1)>0 and len(cpu)>0:
time.sleep(0.5)
print ("\nYour card is \n", (p1[0]))
time.sleep(1.5)
chosenability()
print ("Play again ?")
play_again = input ("Type yes, or no: ")
if play_again == "Yes" or play_again == "YES" or play_again == "yes":
game = True
输出
Your deck is being dealt... please wait
...
Player one has 10 cards... let's do this!
Your card is
Name : Cassie Cage
Rating : 2
Power : 520
Defence : 380
XRay : 900
Toughness : 400
Traceback (most recent call last):
File "H:\GCSE Computer Sciance\Practicle\XMain.py", line 127, in <module>
builtins.TypeError: __str__ returned non-string (type NoneType)
当我运行代码时,它会返回此错误,有人可以帮我修复它吗?
答案 0 :(得分:1)
您的__repr__
方法必须返回一个字符串对象,但不返回任何内容:
def __repr__(self):
print("\nName : ", self.name, "\nRating : ", self.rating, "\nPower : ", self.power, "\nDefence : ", self.defence, "\nXRay : ", self.XRay, "\nToughness : ", self.toughness)
当您使用print ("\nYour card is \n", (p1[0]))
时,p1[0]
卡会被print()
转换为字符串,这意味着p1[0].__str__()
被调用;因为你没有定义那个方法Python然后回到p1[0].__repr__()
,它返回None
并导致错误。
此处不要使用print()
,请使用return
:
def __repr__(self):
return "\nName : {}\nRating : {}\nPower : {}\nDefence : {}\nXRay : {}\nToughness : {}".format(
self.name, self.rating, self.power, self.defence, self.XRay, self.toughness)
答案 1 :(得分:0)
如果不运行代码很难说,但可能是你永远不会为__repr__
方法返回一个字符串,而只是打印出来。
返回您打印的内容,然后在字符串上调用print。