如何使用while循环来给出百分比

时间:2017-10-24 17:36:51

标签: python

我正在尝试制作一个系统,如果你输入一个数字,它会告诉你它会是什么等级,但它不起作用。

我的代码是

percentage = input(“Please input a number”)
if percentage == 100:
     Grade = (“A*”)
     print (“A”)
elif percentage ==80:
     Grade = (“A”)
     print (“A”)
elif percentage == 60:
     Grade = (“B”)
     print (“B”)
elif percentage == 40:
     Grade = (“C”)
     print (“C”)
else:
     Grade == (“D”)
     print (“D”)

错误说

NameError: name ‘grade’ is not defined

帮助,如果我错过了一些明显的事情,请提前抱歉

4 个答案:

答案 0 :(得分:0)

==和=是不同的操作。 ==是比较,而=是赋值。

请尝试以下代码:

telnet 172.17.0.1(docker host) 80

答案 1 :(得分:0)

以下代码在python2.7中工作。

internal class InvoiceGenerationActor : Actor, IInvoiceGenerationActor, IRemindable
{
    protected override async Task OnActivateAsync()
    {
        ////ActorEventSource.Current.ActorMessage(this, "Actor activated.");

        //// The StateManager is this actor's private state store.
        //// Data stored in the StateManager will be replicated for high-availability for actors that use volatile or persisted state storage.
        //// Any serializable object can be saved in the StateManager.
        //// For more information, see https://aka.ms/servicefabricactorsstateserialization

        //// return this.StateManager.TryAddStateAsync("count", 0);

        ////var schedulerDtos = GetSchedulerList();

        await base.OnActivateAsync();

        ActorEventSource.Current.ActorMessage(this, "Actor activated.");

        IActorReminder generationReminderRegistration = await this.RegisterReminderAsync(GenerationRemainder, BitConverter.GetBytes(100), TimeSpan.FromMilliseconds(0), TimeSpan.FromMinutes(10));

        ////IActorReminder mailReminderRegistration = await this.RegisterReminderAsync(generationRemainder, BitConverter.GetBytes(100), TimeSpan.FromMinutes(1), TimeSpan.FromHours(1));

        return;
    }

    public async Task ReceiveReminderAsync(string reminderName, byte[] context, TimeSpan dueTime, TimeSpan period)
    {
        if (reminderName.Equals(GenerationRemainder))
        {
            await GetAllInvoiceSettingAndRegisterNewRemiders();
        }
        else
        {
            int solutionPartnerId = BitConverter.ToInt32(context, 0);
            var data = await Get("SolutionOwnerInvoiceGeneration/AutomaticInvoiceGeneration/" + solutionPartnerId, "PartnerService");
            await UnregisterReminderAsync(GetReminder(reminderName));
        }
    }
}

答案 2 :(得分:0)

请了解Comparisons

我认为这是你想要实现的目标:

percentage = int(input("Please input a number: "))
grade = ''

if percentage == 100:
     grade = "A*"     
elif percentage == 80:
     grade = "A"     
elif percentage == 60:
     grade = "B"     
elif percentage == 40:
     grade = "C"     
else:
     grade = "D"

print(grade)

答案 3 :(得分:0)

这里有很多事情发生。

  1. 您的输入是字符串,但您要将其与int进行比较(始终为false)。
  2. 您只检查确切的百分比,范围怎么样?在您的代码中,输入百分比为81将导致D等级,可能不是您想要的实际输入数据的结果。
  3. 尝试这样的事情:

    from collections import OrderedDict as od
    
    CUTOFFS = od()
    CUTOFFS[100.0] = 'A*'
    CUTOFFS[80.0] = 'A'
    CUTOFFS[60.0] = 'B'
    CUTOFFS[40.0] = 'C'
    CUTOFFS[0.0] = 'D'
    
    def percent_to_grade(p):
        for k, v in CUTOFFS.items():
            if k <= float(p):
                return v
        raise ValueError('Invalid CUTOFFS data')
    
    def get_grade(percent=None):
        while True:
            if percent is not None:
                try:
                    percent = float(percent)
                    if percent < 0.0 or percent > 100.0:
                        percent = None
                        raise ValueError('Invalid value for percent')
                    break
                except Exception as exc:
                    print str(exc), 'Trying again ...'
            percent = input('What is your grade? ')
        ##
        grade = percent_to_grade(percent)
        return grade
    
    def test():
        vals = [100, 80, 79.9, 39, 0, -2, 'TBD', None]
        for v in vals:
            print v, get_grade(v)
            print
    

    运行test功能以查看此操作:

    >>> test()
    100 A*
    
    80 A
    
    79.9 B
    
    39 D
    
    0 D
    
    -2 Invalid value for percent Trying again ...
    What is your grade? 10
    D
    
    TBD could not convert string to float: TBD Trying again ...
    What is your grade? 20
    D
    
    None What is your grade? 30
    D
    

    我给最后三个手动输入,所以我们可以看到所有测试用例。