我绝对是python的初学者,但我真的想挑战自己。我想创建一个游戏,其中每个玩家(目前,两个玩家)掷骰子2次。将骰子的结果相加,如果将骰子的偶数相加10分,如果将其奇数相加,则减去5分。玩家最多可以玩5回合。到目前为止,我将代码保留在while循环中,并导入了随机代码以“掷骰子”,但我不知道如何将随机结果加在一起。
我的代码可能是完全错误的,但是我希望获得有关如何修复它的帮助和建议(这是在python 3上完成的)
我的代码:
person1_name = input("What is your name: ")
person2_name = input("What is your name: ")
import random
number = random.randint(1,6)
rounds = "yes"
while rounds == "yes":
print(person1_name, "- 1st roll = ",number, " and 2nd roll = ",number)
total_1 = number + number
if total_1 % 2 == 0:
total_1 = total_1 + 10
print(person1_name," has ",total_1, "points")
else:
total_1 = total_1 - 5
print(person1_name, " has ",total_1, "points")
print(person2_name, "- 1st roll = ",number, "and 2nd roll = ",number)
total_2 = number + number
if total_2 % 2 == 0:
total_2 = total_2 + 10
print(person2_name," has ",total_2, "points")
else:
total_2 = total_2 - 5
print(person2_name," has ",total_2, "points")
rounds = input("Do you want to play again (yes/no): ")
答案 0 :(得分:3)
您可以直接使用random.choices()获取总和。
File "C:\Python\lib\site-packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "C:\Python\lib\site-packages\django\core\handlers\base.py" in _get_response
126. response = self.process_exception_by_middleware(e, request)
File "C:\Python\lib\site-packages\django\core\handlers\base.py" in _get_response
124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python\lib\site-packages\django\views\decorators\csrf.py" in wrapped_view
54. return view_func(*args, **kwargs)
File "C:\Python\lib\site-packages\rest_framework\viewsets.py" in view
103. return self.dispatch(request, *args, **kwargs)
File "C:\Python\lib\site-packages\rest_framework\views.py" in dispatch
483. response = self.handle_exception(exc)
File "C:\Python\lib\site-packages\rest_framework\views.py" in handle_exception
443. self.raise_uncaught_exception(exc)
File "C:\Python\lib\site-packages\rest_framework\views.py" in dispatch
480. response = handler(request, *args, **kwargs)
File "C:\Python\lib\site-packages\rest_framework\mixins.py" in list
48. return Response(serializer.data)
File "C:\Python\lib\site-packages\rest_framework\serializers.py" in data
765. ret = super(ListSerializer, self).data
File "C:\Python\lib\site-packages\rest_framework\serializers.py" in data
262. self._data = self.to_representation(self.instance)
File "C:\Python\lib\site-packages\rest_framework\serializers.py" in to_representation
683. self.child.to_representation(item) for item in iterable
File "C:\Python\lib\site-packages\rest_framework\serializers.py" in <listcomp>
683. self.child.to_representation(item) for item in iterable
File "C:\Python\lib\site-packages\rest_framework\serializers.py" in to_representation
527. ret[field.field_name] = field.to_representation(attribute)
File "C:\Python\lib\site-packages\rest_framework\serializers.py" in to_representation
683. self.child.to_representation(item) for item in iterable
File "C:\Python\lib\site-packages\rest_framework\serializers.py" in <listcomp>
683. self.child.to_representation(item) for item in iterable
File "C:\Python\lib\site-packages\rest_framework\serializers.py" in to_representation
510. fields = self._readable_fields
File "C:\Python\lib\site-packages\django\utils\functional.py" in __get__
37. res = instance.__dict__[self.name] = self.func(instance)
File "C:\Python\lib\site-packages\rest_framework\serializers.py" in _readable_fields
376. field for field in self.fields.values()
File "C:\Python\lib\site-packages\rest_framework\serializers.py" in fields
363. for key, value in self.get_fields().items():
File "C:\Python\lib\site-packages\rest_framework\serializers.py" in get_fields
1045. source, info, model, depth
File "C:\Python\lib\site-packages\rest_framework\serializers.py" in build_field
1190. return self.build_unknown_field(field_name, model_class)
File "C:\Python\lib\site-packages\rest_framework\serializers.py" in build_unknown_field
1302. (field_name, model_class.__name__)
Exception Type: ImproperlyConfigured at /forms/
Exception Value: Field name `field_type` is not valid for model `FormFields`.
或直接检查:
sum_dices = sum(random.choices(range(1,7),k=2))
答案 1 :(得分:1)
如上所述,您需要调用random.randint()
来在循环内生成随机数。为了保持轮数,请使用另一个变量。试试这个:
import random
person1_name = input("What is your name: ")
person2_name = input("What is your name: ")
rounds = "yes"
person1_tot, person2_tot = 0,0 ##
numround = 1 # Number of rounds
while rounds == "yes" and numround<=5:
number1 = random.randint(1,6)
number2 = random.randint(1,6)
print(person1_name, "- 1st roll = ",number1, " and 2nd roll = ",number2)
total_1 = number1 + number2
if total_1 % 2 == 0:
total_1 = total_1 + 10
print(person1_name," has ",total_1, "points")
else:
total_1 = total_1 - 5
print(person1_name, " has ",total_1, "points")
person1_tot += total_1 ##
print(person1_name, " : ", person1_tot) ##
number1 = random.randint(1,6)
number2 = random.randint(1,6)
print(person2_name, "- 1st roll = ",number1, "and 2nd roll = ",number2)
total_2 = number1 + number2
if total_2 % 2 == 0:
total_2 = total_2 + 10
print(person2_name," has ",total_2, "points")
else:
total_2 = total_2 - 5
print(person2_name," has ",total_2, "points")
person2_tot += total_2 ##
print(person2_name, " : ", person2_tot) ##
numround+=1
rounds = input("Do you want to play again (yes/no): ")
答案 2 :(得分:0)
您应注意创建随机数的位置。在您的代码中,它是不循环的,这意味着在整个会话中,您只会产生一个随机数。我想您也想使用两个模具,所以这意味着您需要另一个数字变量。
import random
person1_name = input("What is your name: ")
person2_name = input("What is your name: ")
rounds = "yes"
while rounds == "yes":
number, number2 = random.randint(1,6), random.randint(1,6)
total_1 = number + number2
print(person1_name, "- 1st roll = ",number, " and 2nd roll = ",number2)
if total_1 % 2 == 0:
total_1 = total_1 + 10
print(person1_name," has ",total_1, "points")
else:
total_1 = total_1 - 5
print(person1_name, " has ",total_1, "points")
number, number2 = random.randint(1,6), random.randint(1,6)
total_2 = number + number2
print(person2_name, "- 1st roll = ",number, "and 2nd roll = ",number2)
if total_2 % 2 == 0:
total_2 = total_2 + 10
print(person2_name," has ",total_2, "points")
else:
total_2 = total_2 - 5
print(person2_name," has ",total_2, "points")
rounds = input("Do you want to play again (yes/no): ")
答案 3 :(得分:-1)
这里有骰子数量和玩家数量:
number_of_players = int(raw_input("Number of players: "))
number_of_dice = int(raw_input("Number of dice: "))
player_names = []
player_points = []
for i in range(number_of_players):
name = raw_input("What is your name: ")
player_names.append(name)
player_points.append(0)
import random
def roll(number_of_dice=2):
total = 0
rolls = []
for i in range(number_of_dice):
dice = random.randint(1,6)
print("dice "+str(i+1)+": "+str(dice))
total += dice
rolls.append(dice)
return rolls,total
rounds = "yes"
while rounds == "yes":
for i in range(number_of_players):
print("rolling dice for "+player_names[i]+"....")
rolls,total = roll(number_of_dice)
if total % 2 == 0:
player_points[i] += 10
else:
player_points[i] -= 5
print(player_names[i]+" has "+str(player_points[i])+" points")
rounds = raw_input("Do you want to play again (yes/no): ")