为什么while循环结束?

时间:2017-12-21 21:50:27

标签: python while-loop

这是我试图运行的代码......

 export class HomePage {
    Products: Product[];
    Baners: Baner[];

    constructor(private navCtrl: NavController,
                private productsService: ProductsService,
                private BannerService: BanersService) {
    }

    /**
     * Get products
     */
    loadProducts(): void {
        this.productsService.getOnHomepage().subscribe((products) => {
            this.Products = products;
        });
    }

    /**
      * get baners    
    */
    loadBaners(): void {
        this.BannerService.getOnHomepage().subscribe((baners) => {
            this.Baners = baners;
        });
    }


    loadData(): void {
        // I want to show the loader

        this.loadProducts();
        this.loadBaners();

        //I want to hide the loader

    }



 ionViewWillEnter(){
        this.loadData();
    }

     }

这是我认为代码停止工作的地方......

scoreCount = int(input("How many scores do you want to record?"))
recordedValues = 0
averageScore = totalScore/scoreCount
highestScore = 0
totalScore = 0

如何让while循环结束并显示平均分数/最高分数/记录值?

编辑:感谢您的帮助,我解决了这个问题。

2 个答案:

答案 0 :(得分:2)

你在while循环中输入==而不是=

data-purplecoat-for

所以'录音值'和' totalScore'没有改变。

修改:' khelwood'已在评论中提及。

答案 1 :(得分:1)

您的代码存在很多问题:在averageScore被定义之前,您尝试将totalScore/scoreCount分配给totalScore,有时您会使用==等式检查器一个赋值运算符,即使它已经被转换,你检查score是否在int中,并且你的while循环中的条件有问题。您可以这样做:

用异常处理替换重复类型测试并删除非法变量赋值:

try:
    scoreCount = int(input("How many scores do you want to record?"))
except ValueError:
    print("\n\nYou need to enter an integer...")
    quit()
recordedValues = 0
highestScore = 0
totalScore = 0

>=更改为>以获得最高分,修复变量分配,并使用异常处理替换不需要的类型检查。

while recordedValues <= scoreCount:
    try:
        score = int(input("\n\nEnter Score: "))
    except ValueError:
        print('Scores must be numbers.')
        quit()
    totalScore += score
    recordedValues += 1
    if score > highestScore:
        highestScore = score

print("\n\nThe amount of values recorded:", recordedValues)
print("\n\nThe average score:", totalScore / scoreCount)
print("\n\nThe highest score:", highestScore)