简体   繁体   English

如何在 Python 中获得变量输入?

[英]How do I get a variable input in Python?

For some context, I'm working on a farenheit to celsius code converter, I also added some extra games and modifications to the code.在某些情况下,我正在开发一个华氏到摄氏度的代码转换器,我还添加了一些额外的游戏和对代码的修改。 But the variable "ContinueYN" will get the "yes" and "no" input, but will not get the "I don't understand input".但是变量“ContinueYN”会得到“yes”和“no”输入,但不会得到“I don't understand input”。

This is my code:这是我的代码:

import random
import time
continueYN = "y"
firstletter = "n"
firstletterCAP = "N"
firstletterY = "y"
firstletterCAPY = "Y"
Revert = "I dont understand"

def Modification():
   x = random.randint(1,100)
   print("Calculate",x,"Fahrenheit to Celsius, The Formular is","(",x," - 32) * 5 / 9, You Have 30 Seconds, Your Time Starts Now!")
   useranswer = int(input())
   time.sleep(30)
   answer = (x - 32) * 5 / 9
   if useranswer == answer:
       print("You Did It!")
       quit()
   else:
       print("Try Again!")
 
while continueYN == "y":
   sDegreeF = input("Enter next temperature in degrees Fahrenheit (F):")
   nDegreeF = int(sDegreeF)
   nDegreeC = (nDegreeF - 32) * 5 / 9
   print ("Temperature in degrees C is:", nDegreeC)
   if nDegreeC < 0:
      print ("Pack long underwear!")
   if nDegreeF > 100:
      print ("Remember to hydrate!")
 
   continueYN = input("Input another?")

   if continueYN[0] == firstletterY or continueYN[0] == firstletterCAPY:
       Modification()
   if continueYN[0] == firstletter or firstletterCAP:
       break
   if continueYN == Revert:
      continueYN = input("Input another?")
   else:
       print("I don't understand!")

       

I am using python 3.10.5, I would appreciate an answer as well as an explanation on what I was doing wrong我正在使用 python 3.10.5,我将不胜感激并解释我做错了什么

Simple syntax error, you missed the second condition on if statement简单的语法错误,您错过了 if 语句的第二个条件

if continueYN[0] == firstletterY or continueYN[0] == firstletterCAPY:
   Modification()

if continueYN[0] == firstletter or continueYN[0] == firstletterCAP:
   break

if continueYN == Revert:
  continueYN = input("Input another?")
else:
  print("I don't understand!")

your if statement should be like this你的 if 语句应该是这样的

Although it becomes confusing as to what you are trying to achieve as I try to understand the functionality of your code.尽管当我尝试理解您的代码的功能时,您试图实现的目标变得令人困惑。 Also your question is a bit unclear too.你的问题也有点不清楚。 Still I try to answer considering, what you want to achieve is, "If a user inputs anything that does not start with "y","Y","N","n", the program should display: I don't understand:": Your code has below issues relating to that:我仍然尝试回答考虑,您想要实现的是,“如果用户输入的任何内容不以“y”、“Y”、“N”、“n”开头,程序应该显示:我不理解:”:您的代码有以下与此相关的问题:

You are not comparing the value in second fi statement, rather you have just put the value which will always be true:您不是在比较第二个 fi 语句中的值,而是您刚刚输入了始终为 true 的值:

Wrong:错误的:

if continueYN[0] == firstletter or firstletterCAP:
            break

Right:正确的:

if continueYN[0] == firstletter or continueYN[0] ==firstletterCAP:
        break

You are comparing the string value to the output you need rather than what user provides.您正在将字符串值与您需要的 output 进行比较,而不是用户提供的值。 Use below piece of code in place of your conditional statements:使用以下代码代替您的条件语句:

if continueYN[0] == firstletterY or continueYN[0] == firstletterCAPY:
    Modification()
if continueYN[0] == firstletter or continueYN[0] ==firstletterCAP:
    break
else:
    print("I don't understand!")
    continueYN = input("Input another?")

Also, the condition that you have for while loop will not be satisfied in all positive scenario.此外,在所有积极的情况下都不会满足您 for while 循环的条件。 Please replace it with below:请将其替换为以下内容:

while(continueYN[0] == firstletterY or continueYN[0] == firstletterCAPY):

You don't really need 'yYnN' style of responses to decide if the input and consequential calculations should continue.您实际上并不需要 'yYnN' 风格的响应来决定是否应该继续输入和相应的计算。 An empty input could be used to indicate that no more input is required whereas anything else is assumed to be a Fahrenheit value.空输入可用于指示不再需要输入,而其他任何输入都假定为华氏值。

You should always validate user input.您应该始终验证用户输入。

This code ignores the OP's Modification() function as its purpose is unclear unless it's giving the user a 1 in 100 of selecting the same random number as returned by randint() .此代码忽略了 OP 的Modification() function,因为它的目的尚不清楚,除非它为用户提供 100 分之一的选择与randint()返回的相同随机数。 Pretty pointless.很没有意义。

Anyway...反正...

while True:
    fs = input('Enter temperature in Fahrenheit (Return to finish): ')
    if fs:
        try:
            f = float(fs)
            c = (f - 32) * 5 / 9
            print(f'The temperature in celcius is {c:.2f}')
            if c < 0:
                print('Pack long underwear!')
            elif f > 100:
                print('Remember to hydrate!')
        except ValueError as e:
            print(e)
    else:
        break

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM