How to display a user prompt in a calculator program - calculator

I'm struggling with a while loop problem. Once the calculation is done, I want the user to be asked whether they want to close the calculator or clear the screen and retry.
How can I use the while loop to do that, if at all?
print ("Calculator Program")
clear="c"
while clear=="c":
one=input("Enter your first number:")
two=input("Enter your second number:")
operator=input("""Choose an operator:
1) Add
2) Subtract
3) Multiply
4) Divide
""")
if operator == '1':
ans=float(one)+float(two)
print (ans)
elif operator == '2':
ans=float(one)-float(two)
print (ans)
elif operator == '3':
ans=float(one)*float(two)
print (ans)
elif operator == '4':
ans=float(one)/float(two)
print(ans)

For the last line in your loop add:
clear = input("[c]lear or [e]xit")
If the user types anything besides "c" the loop will break and the program will finish execution

Related

how to search for a sub string within a string in QBasic

I am creating a simple chat programme in QBasic that will answer questions based on some specific key words present in the user input.therefore I need a way to search for a sub string (I.e. A specific word)within a string.
So, please help me.
To find out if a string contains a certain (sub-)string, you can do this:
text$ = "nonsense !"
IF INSTR( text$, "sense" ) >= 1 THEN
PRINT "This text makes sense !"
END IF
And no, I was not able to test this, as a no longer have QBasic on my PC ;-)
According to the link from the comment above >= 1 is ok
I think INSTR is usually used as follows:
sent$ = "This is a sentence"
PRINT INSTR(1, sent$, "is")
PRINT INSTR(4, sent$, "is")
PRINT INSTR(1, sent$, "word")
the first PRINT command will print a '3' since the first location of "is" within the sentence is at position 3. (The 'is' in 'This')
the second PRINT command starts searching at position 4 (the 's' in 'This'), and so finds the "is" at position 6. So it will print '6'.
the third PRINT command will print a '0' since there is no instance of "word" in the sentence.
Counts the occurrences of a substring within a string.
T$ = "text to be searched and to be displayed"
S$ = "to"
l = 1
DO
x = INSTR(l, T$, S$)
IF x THEN
n = n + 1
l = x + LEN(S$)
ELSE
EXIT DO
END IF
LOOP
PRINT "text '"; S$; "' matches"; n; "times."

I'm new to QBasic and coding in general and I'm making a guessing game that just wont work

I'm new to QBasic and coding in general and I'm making a guessing game that just won't work.
I have to make a guessing game that does not use GOTO or Do statements, and gives the user 5 chances. Here's the code:
chances%=1
dim guess as integer
dim answer as string
randomize timer
rndnum=INT(RND*100+1)
'makinng a title
color 5
locate 12,32
print "welcome to My guessing game."
Print "think of a number between 1 and 100."
color 12
Input "enter you guess: ",guess
while chances%<4
if guess >rndnum then
print "wrong, too high"
elseif guess <rndnum then
print "wrong, too low"
elseif guess=rndnum then
print "your guessed the number!"
end if
wend
chances%=chances%+1
color 14
Print "you took "; chances%;"to guess the number"
color 3
Input would you like to play again (yes/no)?", answer
wend
if answer = "yes" then
?
else
print "have a good day"
end if
end
You are asking for input one time, then you have a closed loop that checks the answer until attempts are greater than four, but attempts does not ever increment because the Wend command tells it to start the loop again without asking the question again or incrementing the counter at all. This is what is called an "endless loop" because the conditions inside the loop will not change. I'll leave it at that and see if you can figure out how to correct both of these issues - note that fixing only one of them will not stop it from being an "endless loop" you must solve both.
You could use WHILE...WEND to run a loop until chances becomes 0.Here is what I mean:
....(rest of code)
chances = 5
WHILE chances > 0
....
if guess > rndnum then
print "wrong, too high"
chances = chances - 1
elseif guess < rndnum then
print "wrong, too low"
chances = chances -1
....
WEND
Your guess must be inside the while wend loop and when the correct answer is given chances% must be set equal to 4 otherwise you end up with an eternal loop.
It is also necessary to increment chances% directly after the first guess. See the slightly changed code. Please also see guesses and change your line saying you took x guess from chances% to guesses
chances%=0
while chances% < 4
Input "enter your guess: ",guess
chances% = chances% + 1
if guess > rndnum then
print "wrong, too high"
elseif guess < rndnum then
print "wrong, too low"
elseif guess = rndnum then
print "your guessed the number!"
guesses = Chances%
chances% = 4
end if
wend
if you're still having problems I found that here:
Input would you like to play again (yes/no)?", answer
...
if answer = "yes"
...
you would have to change answer to answer$ because you cannot save a string inside a number value.
This snip demonstrates a number guessing game in QB64:
REM guessing game.
Tries = 5
DO
PRINT "Guess a number between 1 and 100 in"; Tries; "guesses."
Number = INT(RND * 100 + 1)
Count = 1
DO
PRINT "Enter guess number"; Count; " ";
INPUT Guess
IF Guess = Number THEN
PRINT "Correct! You guessed it in"; Count; "tries."
EXIT DO
END IF
IF Guess > Number THEN
PRINT "Wrong. Too high."
ELSE
PRINT "Wrong. Too low."
END IF
Count = Count + 1
IF Count > Tries THEN
PRINT "The number was"; Number
PRINT "You didn't guess it in"; Tries; "tries."
EXIT DO
END IF
LOOP
DO
PRINT "Play again(yes/no)";
INPUT Answer$
IF LCASE$(Answer$) = "no" THEN
END
END IF
IF LCASE$(Answer$) = "yes" THEN
EXIT DO
END IF
LOOP
LOOP
END

Expected:: - Using Eclipse with PyDev

I just started up the new version of python, and realized that a lot has changed. Anyways, eclipse comes up with a red "X" mark beside the line numbers saying "Expected::". Could someone please explain what this means, and how I can get rid of it?
This is the code I'm trying to make work with Eclipse and the new Python version:
print "Please insert a valid operator that you want to calculate with."
print "Valid operators are +, -, : and *"
operator = str(raw_input("What's your operator? "))
numb1 = int(raw_input("Please insert the first number:"))
numb2 = int(raw_input("Please insert the second number:"))
if operator == "+":
print numb1 + numb2
elif operator == "*":
print numb1 + numb2
elif operator == "-":
print numb1 - numb2
elif operator == "/":
print numb1 / numb2
On Python3, print is a function, not a statement, so it should be written (for example)
print("Please insert a valid operator that you want to calculate with.")
Also raw_input has been renamed to input so it should be (for example):
numb1 = int(input("Please insert the first number:"))
I ran this program and I saw no problems with it when I ran it on Pydev on Eclipse, even though I ran it on 2.7. perhaps it has something to do with your indentation.
operator = str(raw_input("What's your operator? "))
numb1 = int(raw_input("Please insert the first number:"))
numb2 = int(raw_input("Please insert the second number:"))
if operator == "+":
print numb1 + numb2
elif operator == "*":
print numb1 + numb2
elif operator == "-":
print numb1 - numb2
elif operator == "/":
print numb1 / numb2
I copy pasted your example as well and to fix it I just had to fix the indentation as StackXchangeT did.
However I got Expected:: error when there was missing finishing : at the end of declaration e.g.:
class MyInvalidClass
which is supposed to be:
class MyInvalidClass:
Perhaps you get such error in similar situations where : is needed (just a guess).

Trouble using 'while' loop to evaluate multiple lines, Perl

Thank you in advance for indulging an amateur Perl question. I'm extracting some data from a large, unformatted text file, and am having trouble combining the use of a 'while' loop and regular expression matching over multiple lines.
First, a sample of the data:
01-034575 18/12/2007 258,750.00 11,559.00 36 -2 0 6 -3 2 -2 0 2 1 -1 3 0 5 15
-13 -44 -74 -104 -134 -165 -196 -226 -257 -287 -318 -349 -377 -408 -438
-469 -510 -541 -572 -602 -633 -663
Atraso Promedio ---> 0.94
The first sequence, XX-XXXXXX is a loan ID number. The date and the following two numbers aren't important. '36' is the number of payments. The following sequence of positive and negative numbers represent how late/early this client was for this loan at each of the 36 payment periods. The '0.94' following 'Atraso Promedio' is the bank's calculation for average delay. The problem is it's wrong, since they substitute all negative (i.e. early) payments in the series with zeros, effectively over-stating how risky a client is. I need to write a program that extracts ID and number of payments, and then dynamically calculates a multi-line average delay.
Here's what I have so far:
#Create an output file
open(OUT, ">out.csv");
print OUT "Loan_ID,Atraso_promedio,Atraso_alt,N_payments,\n";
open(MYINPUTFILE, "<DATA.txt");
while(<MYINPUTFILE>){
chomp($_);
if($ID_select != 1 && m/(\d{2}\-\d{6})/){$Loan_ID = $1, $ID_select = 1}
if($ID_select == 1 && m/\d{1,2},\d{1,3}\.00\s+\d{1,2},\d{1,3}\.00\s+(\d{1,2})/) {$N_payments = $1, $Payment_find = 1};
if($Payment_find == 1 && $ID_select == 1){
while(m/\s{2,}(\-?\d{1,3})/g){
$N++;
$SUM = $SUM + $1;
print OUT "$Loan_ID,$1\n"; #THIS SHOWS ME WHAT NUMBERS THE CODE IS GRABBING. ACTUAL OUTPUT WILL BE WRITTEN BELOW
print $Loan_ID,"\n";
}
if(m/---> *(\d*.\d*)/){$Atraso = $1, $Atraso_select = 1}
if($ID_select == 1 && $Payment_find == 1 && $Atraso_select == 1){
...
There's more, but the while loop is where the program is breaking down. The problem is with the pattern modifier, 'g,' which performs a global search of the string. This makes the program grab numbers that I don't want, such as the '1' in loan ID and the '36' for the number of payments. I need the while loop to start from wherever the previous line in the code left off, which should be right after it has identified the number of loans. I've tried every pattern modifier that I've been able to look up, and only 'g' keeps me out of an infinite loop. I need the while loop to go to the end of the line, then start on the next one without combing over the parts of the string already fed through the program.
Thoughts? Does this make sense? Would be immensely grateful for any help you can offer. This work is pro-bono, unpaid: just trying to help out some friends in a micro-lending institution conduct a risk analysis.
Cheers,
Aaron
The problem is probably easier using split, for instance something like this:
use strict;
use warnings;
open DATA, "<DATA.txt" or die "$!";
my #payments;
my $numberOfPayments;
my $loanNumber;
while(<DATA>)
{
if(/\b\d{2}-\d{6}\b/)
{
($loanNumber, undef, undef, undef, $numberOfPayments, #payments) = split;
}
elsif(/Atraso Promedio/)
{
my (undef, undef, undef, $atrasoPromedio) = split;
# Calculate average of payments and print results
}
else
{
push(#payments, split);
}
}
If the data's clean enough, I might approach it by using split instead of regular expressions. The first line is identifiable if field[0] matches the form of a loan number and field[1] matches the format of a date; then the payment dates are an array slice of field[5..-1]. Similarly testing the first field of each line tells you where you are in the data.
Peter van her Heijden's answer is a nice simplification for a solution.
To answer the OP's question about getting the regexp to continue where it left off, see Perl operators - regexp-quote-like operators, specifically the section "Matching in list context" and the "\G assertion" section just after that.
Essentially, you can use m//gc along with the \G assertion to use regexps match where previous matches left off.
The example in the "\G assertion" section about lex-like scanners would seem to apply to this question.

How can I replace empty elements in an array with "OTHER"?

My list (#degree) is built from a SQL command. The NVL command in the SQL isn't working, neither are tests such as:
if (#degree[$i] == "")
if (#degree[$i] == " ")
if (#degree[$i] == '')
if (#degree[$i] == -1)
if (#degree[$i] == 0)
if (#degree[$i] == ())
if (#degree[$i] == undef)
$i is a counter variable in a for loop. Basically it goes through and grabs unique degrees from a table and ends up creating ("AFA", "AS", "AAS", "", "BS"). The list is not always this long, and the empty element is not always in that position 3.
Can anyone help?
I want to either test during the for loop, or after the loop completes for where this empty element is and then replace it with the word, "OTHER".
Thanks for anything
-Ken
First of all, the ith element in an array is $degree[$i], not #degree[$i]. Second, "==" is for numerical comparisons - use "eq" for lexical comparisons. Third of all, try if (defined($degree[$i]))
Everything that Paul said. And, if you need an example:
my #degree = ('AFA', 'AS', 'AAS', '', 'BS');
$_ ||= 'OTHER' for #degree;
print join ' ' => #degree; # prints 'AFA AS AAS OTHER BS'
If its actually a null in the database, try COALESCE
SELECT COALESCE(column, 'no value') AS column FROM whatever ...
That's the SQL-standard way to do it.