Я бы посоветовал вам переписать код - например, вы можете использовать цикл for
вместо цикла while
для отслеживания циклов. Я думаю, что использование dict
или list
полезно. Я добавил два словаря для обоих игроков, result_p1
и result_p2
, которые хранят оценки за раунд. Оценка может быть отрицательной (не уверен, если задумано).
Вот код ниже:
import random
import time
def bothDice():
count=0
# New dicts that store counts
result_p1 = {}
result_p2 = {}
while count<5:
count=count+1
score=0
print("Round",count)
print("Player One rolls first dice")
time.sleep(1)
dice1=(random.randint(1,6))
print("you rolled a ",dice1)
print("Player One rolls second dice")
time.sleep(1)
dice2=(random.randint(1,6))
print("you rolled a ",dice2)
score=dice1+dice2
if score%2==0:
score=score+10
else:
score=score-5
if dice1==dice2:
print("You rolled a double- You get an extra roll")
for x in range (1):
print("You rolled a:")
extraDice=(random.randint(1,6))
print(extraDice)
extraScore = score+extraDice
score = extraScore
print("======","Your combined score is ", score,"======")
# Store result of this round for player 1
if score != 0:
result_p1[count] = score
else:
result_p1[count] = 0
score=0
print("Player Two rolls first dice")
time.sleep(1)
dice1=(random.randint(1,6))
print("you rolled a ",dice1)
print("Player Two rolls second dice")
time.sleep(1)
dice2=(random.randint(1,6))
print("you rolled a ",dice2)
score=dice1+dice2
if score%2==0:
score=score+10
else:
score=score-5
if dice1==dice2:
print("You rolled a double- You get an extra roll")
for x in range (1):
print("You rolled a:")
extraDice=(random.randint(1,6))
print(extraDice)
extraScore = score+extraDice
score = extraScore
print("======","Your combined score is ", score,"======")
# Store result of this round for player 2
if score != 0:
result_p2[count] = score
else:
result_p2[count] = 0
# Print sum of results using f-string in python 3.
print(f"Player 1 scores: {result_p1}")
print(f"Player 2 scores: {result_p2}")
print(f"Sum of player 1 score: {sum(result_p1.values())}")
print(f"Sum of player 2 score: {sum(result_p2.values())}")
def main():
bothDice()
main()
Этот может быть полезен, если мое утверждение для печати не имело смысла.
Вот вывод новых операторов печати.
Player 1 scores: {1: 9, 2: 13, 3: 17, 4: 0, 5: 19}
Player 2 scores: {1: 17, 2: 13, 3: 13, 4: -2, 5: -2}
Sum of player 1 score: 58
Sum of player 2 score: 39
Редактировать: Добавлено if
высказываний перед добавлением очков игроку dict
, чтобы проверить, является ли score
отрицательным. Если отрицательно -> вместо этого добавьте 0
.
Используйте столбец адреса. Я предполагаю, что столбец человека проигнорирован, потому что люди могут отправить SMSs в номера телефона, которые не находятся в списке контактов.
// address contains the phone number
Uri phoneUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, address);
if (phoneUri != null) {
Cursor phoneCursor = getContentResolver().query(phoneUri, new String[] {Phones._ID, Contacts.Phones.PERSON_ID}, null, null, null);
if (phoneCursor.moveToFirst()) {
long person = phonesCursor.getLong(1); // this is the person ID you need
}
}