Python不调用变量(pycharm)

2024-06-16 10:16:11 发布

您现在位置:Python中文网/ 问答频道 /正文

有人知道在代码开头调用输入有什么不对吗?我让某人输入他们希望继续进行的号码,因为某些原因它不想这样做

__author__ = 'kowalczk'
print ("Welcome to my Program")
print ("It has many functions. If you wish to:")
print ("- Interact with the PC - Press 1")
print ("- Add, Subtract, Multiply Or Divide two numbers - Press 2")
print ("- Put any number of words in alphabetical order - Press 3")
print ("- Display the exact date and time - Press 4")
print ("- Count to any number you like - Press 5")
print ("- Cycle through the whole program - Press 6")
Command = float(input())


if Command == "1":
    negative = "angry","depressed","confused","helpless","irritated","lousy","upset","incapable","frustrated","resentful","disgusting","distrustful","distressed","inflamed","abominable","misgiving","woeful","provoked","terrible","lost","pathetic","incensed","in despair","unsure","tragic","infuriated","sulky","uneasy","in a stew","cross","bad","pessimistic","dominated","worked up","a sense of loss","tense","boiling","fuming","indignant","indifferent","afraid","hurt","sad","insensitive","fearful","crushed","tearful","dull","terrified","tormented","sorrowful","nonchalant","suspicious","deprived","pained","neutral","anxious","pained","grief","reserved","alarmed","tortured","anguish","weary","panic","dejected","desolate","bored","nervous","rejected","desperate","preoccupied","scared","injured","pessimistic","cold","worried","offended","unhappy","disinterested","frightened","afflicted","lonely","lifeless","timid","aching","grieved","shaky","victimized","mournful","restless","heartbroken","dismayed","doubtful","agonized","threatened","appalled","cowardly","humiliated","quaking","wronged","menaced","alienated","wary"
    positive = "open","happy","alive","good","understanding","great","playful","calm","confident","gay","courageous","peaceful","reliable","joyous","energetic","at ease","easy","lucky","liberated","comfortable","amazed","fortunate","optimistic","pleased","free","delighted","provocative","encouraged","sympathetic","overjoyed","impulsive","clever","interested","gleeful","free","surprised","satisfied","thankful","frisky","content","receptive","important","animated","quiet","accepting","festive","spirited","certain","kind","ecstatic","thrilled","relaxed","satisfied","wonderful","serene","glad","free and easy","cheerful","bright","sunny","blessed","merry","reassured","elated","jubilant","love","interested","positive","strong","loving","concerned","eager","impulsive","considerate","affected","keen","free","affectionate","fascinated","earnest","sure","sensitive","intrigued","intent","certain","tender","absorbed","anxious","rebellious","devoted","inquisitive","inspired","unique","attracted","nosy","determined","dynamic","passionate","snoopy","excited","tenacious","admiration","engrossed","enthusiastic","hardy","warm","curious","bold","secure","touched","brave","sympathy","daring","close","challenged","loved","optimistic","comforted","re-enforced","drawn toward","confident","hopeful"
    print ("Please tell me your name")
    name = input()
    print ("So", name, ",how are you feeling today?")
    feeling = input().lower()
    while feeling is "":
        print ("How are you feeling today?")
    reason = input()
    if feeling in negative:
        print ("Why is that", name)
    reason = input()
    while reason is "":
        print ("Why is that?")
    reason = input()
    elif feeling in positive:
    print ("That's great", name)


elif Command == "2":
    print ("In this section of my program i will calculate anything that you need me to, press enter to continue")
    input()

    print ("If you wish to add press 1, subtract press 2, multiply press 3 or divide press 4. If you wish to exit press enter")
    operation = input()

if operation == "1":
    print ("Input first number")
    n1 = float(input())
    print ("Input second number")
    n2 = float(input())
    print (n1, "add", n2, "is", n1 + n2)

if operation == "2":
    print ("Input first number")
    n1 = float(input())
    print ("Input second number")
    n2 = float(input())
    print (n1, "takeaway", n2, "is", n1 - n2)

if operation == "3":
    print ("Input first number")
    n1 = float(input())
    print ("Input second number")
    n2 = float(input())
    print (n1, "times by", n2, "is", n1 * n2)

if operation == "4":
    print ("Input first number")
    n1 = float(input())
    print ("Input second number")
    n2 = float(input())
    print (n1, "divided by", n2, "is", n1 / n2)


elif Command == "3":
print ("Now i will put some words in order for you. please tell me how much words you wish me to put in order for you")
amount = int(input())
#create a list
mywords = list()

#use a simple counter
counter = 0

# loop through
while counter < amount:
   #get input from user
   word = input('Enter word: ')
   #add the word to the list
   mywords.append(word)
   #add one to the counter
   counter = counter + 1

#get creative with sort etc.
print (sorted(mywords))


elif Command == "4":
print ("This is the exact date and time. In the form yyyy-mm-dd and hh-mm-ss-ms")
print("Press enter to show")
input()
#This code prints date and time originally in the form yyyy-mm-dd
#hh:mm:ss.ssssss
from datetime import datetime
now = datetime.now()
print (now)
print("This is the date in the form dd-mm-yyyy")
print("Press enter to show")
input()
#This shows date in the format dd-mm-yyyy
print (now.day, -  now.month, -  now.year)


elif Command == "5":
print ("Now i will count to any number you wish me to count to")
print("Press enter to continue")
input()
print ("Tell me what number you wish me to count to")
CountTo = float(input())
x = 0
while x < CountTo:
    x = x + 1
    print (x)

我也得到了一个关于 如果感觉积极: 因为某些原因,我不能让它发挥作用


Tags: thetoinyounumberinputisfloat
1条回答
网友
1楼 · 发布于 2024-06-16 10:16:11

您正在将input()返回值转换为float,行-

Command = float(input())

然后根据字符串检查这个Command,行如下-

^{pr2}$

它们将不匹配,因为字符串永远不会等于float。你不需要做浮动转换-

Command = input()

另外,你应该修正缩进,缩进在Python中非常重要,它是用来定义块的。在

每个单独的块都应该从上一个块的特定级别缩进,建议缩进4个空格。在


对于评论中提到的第二个问题-

line 29 elif feeling in positive: ^ SyntaxError: invalid syntax

问题是一致的-

if feeling in negative:
    print ("Why is that", name)
reason = input()
while reason is "":
    print ("Why is that?")
reason = input()
elif feeling in positive:

这是因为有一个elif部分没有if部分。最有可能的情况是,您想缩进if块中的while部分,如果是的话,就将其缩进。在

相关问题 更多 >