如何将我的python文件转换为int?

时间:2017-04-25 13:32:08

标签: python-3.x

该计划应该以75%的几率将玩家数量翻倍,并以25%的几率减少1。如果新玩家获得的分数高于最后一名玩家,则将玩家姓名与分数一起保存到文件中。一旦玩家失去5分,该程序就会退出。

class ViewController: UIViewController {

    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var tableView: UITableView!
}

extension ViewController: UIScrollViewDelegate {
  func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    if scrollView == tableView {
      print("tableview")
    } else if scrollView == collectionView {
      print("collectionview")
    }
  }
}

底部是文本文件“score.txt”,其中包含以下内容:

import random

points = 1
play = "yes"
name = input("what is your name?")
counter = 0
while play.lower() == "yes":
    risk = random.randint(0,3)
    play = input(name.upper() + "!...want to risk it to bisk it?")
    if risk == 0:
        points -= 1
        counter += 1
        if counter == 5:
            break
    else:
        points *= 2
    fi = open("score.txt", "r")
    score = fi.readlines()
    fi.close()
    scores = int(score[0])
    if (scores > points):
        fi = open("score.txt","w")
        fi.write(points + ":" + name)
        fi.close()
    print(score)

1 个答案:

答案 0 :(得分:0)

你的意思是将数据作为可用的int读回来吗?

with open('score.txt') as F:
    scores = F.readlines()
    scores = [s.strip().split(':') for s in scores]

现在,如果你想循环分数数据:

for score, name in scores:
    print(f"{name} scored:\t {int(score)}")