Python Forum
Where did I get it wrong?!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Where did I get it wrong?!
#1
Hello Pythoners

In my last assignment I received the following task.

Step 1:

I need to write a code that ask the user to input either 'A' or 'T'. Any other input means the user will receive a notification that their input is invalid and the input message please insert 'A' or 'T' appears again.

step 2:

if the user insert 'A' or 'a' or 't' or 'T', a new input ask the user to insert an integer or 'Q'. The program will keep accumulating the integers inserted until 'q' or 'Q' is inserted. any other input than integer the user will be notified that the input is invalid and it will ask them again to insert an integer or 'Q' Then

Step 3:

if the user inserted A in step 1, the output will show all the inserted integers in separate lines and at the end the total, example:

4
5
Total
9

if the user inserted T in step 2, the output will show the total only, example:

Total
9

Note:
Only these keywords are allowed:
if
elif
while
input
print
else
def

Now I did not pass the assignment but I still don't know why I keep getting it wrong. Wall

Here is my code:

items = 0
def omode():
    mode = input('Please enter "A" or "T" ')
    return mode
if omode().isupper() == "T":
    items = input('Please input an integer or Q ')
    while items.isdigit() == True:
        totala = int(items) + int(items)
        items = input('Please input an integer or Q ')
        if items.isdigit() == False:
            if str(items).isupper() == "Q":
                print("items \n", totala)
            else:
                print(items,'is an invalid input')
                items = input('Please input an integer ')
elif omode() == "A":
    items = input('Please input an integer or Q ')
    while items.isdigit() == True:
        totala = int(items) + int(items)
        items = input('Please input an integer or Q ')
        print(items)
        if items.isdigit() == False:
            if str(items).isupper() == "Q":
                print (totala)
            else:
                print(items,'is an invalid input')
                itmes = input('Please input an integer or Q ')
else:
    while omode() != "T" or omode() != "A":
        print("This is an ivalid input, please insert A or T")
        omode()
Reply
#2
The problem is that when user enter something different from 'A', 'T' or 't' (note the difference in your code when it check for 'T' and 'A') it will enter a loop that will continue until user enters 'T' or 'A' and once this is done it will exit the script without doing anything.
You need to do following:
1. Make a loop that will run until user enters 'T' or 'A', otherwise show message then ask for input again.
2. after user entered 'T' or 'A', perform the required action as per the assignment
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(May-09-2018, 11:39 AM)buran Wrote: The problem is that when user enter something different from 'A', 'T' or 't' (note the difference in your code when it check for 'T' and 'A') it will enter a loop that will continue until user enters 'T' or 'A' and once this is done it will exit the script without doing anything.
You need to do following:
1. Make a loop that will run until user enters 'T' or 'A', otherwise show message then ask for input again.
2. after user entered 'T' or 'A', perform the required action as per the assignment

I am trying but I think I am lost between these loops.
Reply
#4
(May-09-2018, 11:50 AM)Tamimi1982 Wrote: I am trying but I think I am lost between these loops.
Show us what you have tried? How did you change your code?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
I am still trying and nothing happened. What is wrong?!!!

items = 0
def omode():
    mode = input('Please enter "A" or "T" ')
    return mode
if omode().isupper() == "T":
    items = input('Please input an integer ')
while items.isdigit():
    totala = int(items)
    totala += totala
    items = input('Please input an integer ')
    if items.isdigit() == False:
        if str(items) == "q" or str(items) == "Q":
            print(totala)
        else:
            print(items,"is an invalid input")
if omode().isupper() == "A":
    items = input('Please input an integer ')
while items.isdigit():
    totala = int(items)
    totala += totala
    items = input('Please input an integer')
    if items.isdigit() == False:
        if str(items) == "q" or str(items) == "Q":
            print(totala)
        else:
            print(items,"is an invalid input")
else:
    print("This is an ivalid input")
    omode()
Reply
#6
Ok, I think I made some progress. But still I have three problems:

1- The total is wrong. For example, I entered 5 and 9 the total added up to 18 not 14

Please enter "A" or "T" t
Please input an integer or Q 5
Please input an integer or Q 9
Please input an integer or Q q
Total
18

2- When I chose A I can't get it to print each integer on a line, using the example above. I want it to be something like this

items
5
9
Total
14

3- The final issue with a wrong entry. If someone enters other than a or t, how to show a message it is a wrong and restart form the
beginning. Example:

This is an invalid entry
Please input A or T ----> the input dialogue box
and continue.

It seems my else statement at the end stop and I don't know how to tell the program restart from the beginning.

items = 0
def omode():
    mode = input('Please enter "A" or "T" ')
    return mode
if omode().capitalize() == "T":
    items = input('Please input an integer or Q ')
    while items.isdigit() == True:
        totala = int(items) + int(items)
        items = input('Please input an integer or Q ')
        if items.isdigit() == False:
            if str(items).capitalize() == "Q":
                print("items \n", totala)
            else:
                print(items,'is an invalid input')
                items = input('Please input an integer ')
elif omode().capitalize() == "A":
    items = input('Please input an integer or Q ')
    while items.isdigit() == True:
        totala = int(items) + int(items)
        items = input('Please input an integer or Q ')
        print(items)
        if items.isdigit() == False:
            if str(items).capitalize() == "Q":
                print (totala)
            else:
                print(items,'is an invalid input')
                itmes = input('Please input an integer or Q ')
else:
    print("This is an ivalid input, please insert A or T")
    omode()
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020